The O.G. Virtual learning! Buy one, get 2 more Lifetime Memberships! - Click Here!

Question about Joystick usage when camera follows player.

Home 4 Forums Swift and Sprite Kit Question about Joystick usage when camera follows player.

This topic contains 2 replies, has 2 voices, and was last updated by  David Romine 3 years, 7 months ago.

  • February 5, 2018 at 1:37 pm #179834

    Hi guys,

    I went through your controls tutorial and was working on a project of my own and am a bit stuck. I want to have a Joystick that moves the character around, plenty of examples on your site on how to do that and I have that working fine. The problem is I also want to have a camera follow the player. So therefore in my touchesMoved in the examples I downloaded you reference a base image which has a ball on top. If I add these as children to the camera the camera follows the player, my Joystick moves along with me but it retains the original x & y locations so my Joystick no longer works correctly. I’m unsure if there’s a new way to get updated locations or I’ll have to figure out a conversion. Looking for some guidance. I’m including the bit of code below which determines and Joysticks position and moves the player. Please let me know if you have questions, thanks in advance.

    // MARK: Touches Moved
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch in (touches ) {
                let location = touch.location(in: self)
                if (walking == true) {
                    // Origional
                    let v = CGVector(dx: location.x - joystick.position.x, dy: location.y - joystick.position.y)
                    let angle = atan2(v.dy, v.dx)
                    _ = angle * CGFloat( 180 / Double.pi)
                    // leaving degree in for reference
                    //println( deg + 180 )
                    //let length:CGFloat = controllerBase.frame.size.height / 2.5
                    let length:CGFloat = 30
                    let xDist:CGFloat = sin(angle - 1.57079633) * length
                    let yDist:CGFloat = cos(angle - 1.57079633) * length
                    if (joystick.frame.contains(location)) {
                        controller.position = location
                    else {
                        controller.position = CGPoint( x: joystick.position.x - xDist, y: joystick.position.y + yDist)
  • February 6, 2018 at 11:41 am #179878

    Yeah, you just have to convert the location around. It might be something like this…

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            /* Called when a touch begins */
            for touch in (touches ) {
                let location = touch.location(in: self)
                let convertedLocation:CGPoint = self.convert(location, to: theCamera)
    //now you can use the converted location

    I’m looking at the code in the Platform Games Kit, and on touch down I moved the ball and base of the virtual joystick to that converted location. Then in the touchesMoved statement I do the same thing.

You must be logged in to reply to this topic.

Know what an affiliate program is? You make money just by sharing links to our site! Win. Win.

Earn when you refer any buyer here! 30 day tracking. Commissions are 33%-50% and recur on subscription products!

Uh oh, missing your affiliate ID. Create one real quick