简体   繁体   中英

How to rotate a Sprite 90 degrees in SpriteKit

Im trying to add rotation to a sprite by 90 degrees, so if i tap above it, it moves 90 degrees an faces that direction. Think like snake game.

In my touchesBegan method i can get the Sprite to rotate, however it seems to flip itself and not actually go 90 degrees, more like 180.

Any ideas why and how I could get it to point in the correct direction.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first as UITouch!
    let touchLocation = touch.locationInNode(self)
    sceneTouched(touchLocation)

    if (car.position.x > touchLocation.x) {
        car.zRotation = CGFloat(M_PI_2)
    } else {
        car.zRotation = CGFloat(-M_PI_2)
    }
}

Based on the fact that your code works for me I would say that this is probably happening because the current value of car.zRotation is not 0.0 but rather something like -M_PI_2 . So when you set it to M_PI_2 you get a rotation of 180 degrees. Try to print car.zRotation before you set a new value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM