Rotate a sprite to sprite position not exact in SpriteKit with Swift

后端 未结 3 382
既然无缘
既然无缘 2021-01-13 02:33

I\'m trying to rotate a player to the touch. Using the touches move function, I set the location to a variable. I then call a function. It works but the angle is off. Locati

3条回答
  •  生来不讨喜
    2021-01-13 02:59

    According to the SpriteKit Best Practices doc

    Use game logic and art assets that match SpriteKit’s coordinate and rotation conventions. This means orienting artwork to the right. If you orient the artwork in some other direction, you need to convert angles between the conventions used by the art and the conventions used by SpriteKit.

    Since the default spaceship points upward (when zRotation is 0), you will need to offset your angle by 90 degrees (pi/2 radians) so that the ship faces to the right when zRotation is zero:

    player?.zRotation = angle - CGFloat(M_PI_2)
    

    Alternatively, you can rotate the spaceship to the right. To rotate the image, click on Assets.xcassets and then click on Spaceship. Right click on the Spaceship image and select the Open in External Editor menu item. This will open the image in the Preview app. In Preview, select Tools > Rotate Right and quit Preview. By rotating the artwork, your code should work without any changes.

提交回复
热议问题