简体   繁体   English

如何在SpriteKit中将Sprite旋转90度

[英]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. 我试图将精灵增加90度的旋转,因此如果我在精灵上方点击,它将朝该方向移动90度。 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. 在我的touchesBegan方法中,我可以使Sprite旋转,但是它似乎会自动翻转而不实际旋转90度,更像是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 . 基于您的代码对我car.zRotation的事实,我可以说这可能是因为car.zRotation的当前值不是0.0而是类似-M_PI_2 So when you set it to M_PI_2 you get a rotation of 180 degrees. 因此,将其设置为M_PI_2您会旋转180度。 Try to print car.zRotation before you set a new value . 设置新值之前,请尝试打印car.zRotation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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