Search code examples
swiftsprite-kitjoystick

How to move sprite with joystick?


I want to know how can I move the player with a joystick to all directos depending the movement of the joystick. I already made the joystick, and is already setup with degrees. But how I move the player to all direction? Thanks.

class GameScene: SKScene {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")
let base = SKSpriteNode(imageNamed: "circuloFondo")
let ball = SKSpriteNode(imageNamed: "circulo")
var stickActive:Bool = false



override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    base.size = CGSize(width: 100, height: 100)
    base.alpha = 0.3
    base.zPosition = 2.0
    base.position = CGPoint(x: frame.width / 2, y: frame.height / 2 - 310)
    self.addChild(base)

    ball.size = CGSize(width: 50, height: 50)
    ball.color = circuloPrincipal.color
    //ball.alpha = 0
    ball.zPosition = 3.0
    ball.position = base.position
    self.addChild(ball)

    circuloPrincipal.size = CGSize(width: 35, height: 35)
    circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
    self.addChild(circuloPrincipal)
    circuloPrincipal.color =  UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 1.0)
    circuloPrincipal.colorBlendFactor = 1.0
    circuloPrincipal.zPosition = 3.0
}

override func touchesBegan(touches: Set<UITouch>, withEvent event:     UIEvent?) {


    for touch in touches {
        let location = touch.locationInNode(self)

        if (CGRectContainsPoint(ball.frame, location)) {

            stickActive = true
        }else {

            stickActive = false

        }

    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)

        if (stickActive == true) {


        let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
        let angle = atan2(v.dy, v.dx)
        let deg = angle * CGFloat(180 / M_PI)
        print(deg + 180)

        let lenght:CGFloat = base.frame.size.height / 2 - 20
        let xDist: CGFloat = sin(angle - 1.57079633) * lenght
        let yDist: CGFloat = cos(angle - 1.57079633) * lenght

        ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

        if (CGRectContainsPoint(base.frame, location)) {

            ball.position = location
        }else {

            ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

        }


        } // termina stickActive

    }

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if (stickActive == true) {

        let move: SKAction = SKAction.moveTo(base.position, duration: 0.2)
        move.timingMode = .EaseOut

        ball.runAction(move)

    }
}

Solution

  • this is the gist

    // this is position of your joystick knob
    let joyStickPos = CGPointMake(20, 30)
    let length = sqrt(joyStickPos.x * joyStickPos.x + joyStickPos.y*joyStickPos.y)
    let normalizedPoint = CGPointMake(joyStickPos.x/length, joyStickPos.y/length)
    
    
    // in update method
    let speed = CGFloat(10)
    sprite.position.x += (normalizedPoint.x * speed)
    sprite.position.y += (normalizedPoint.y * speed)