How to rotate an SKSpriteNode around the node's Y-axis?

后端 未结 4 2030
执笔经年
执笔经年 2020-12-28 20:37

I\'m trying to rotate an SKSpriteNode node around it\'s Y-axis. I know there\'s the zRotation property and that will rotate the node clockwise or counter-clockwise; however,

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 21:30

    Although SpriteKit doesn't have full 2.5D capabilities (rotating and transforming along X, Y and Z axes), I've developed a work around for rotating SKSpriteNodes around either the X or Y axes. Because SKView is a subclass of UIView, we can rotate it in 3D space the same way we rotate a UIView, using CATransform3D.

    The main point is that the sprite that needs to be rotated on the Y axis lives in its own scene, which is presented in its own SKView.

    Triggering the following code on in a loop will animate your SKView (containing the SKScene and the SKSpriteNode) on the y axis. You can still have your main scene running at the same time in the background and swap in the sprites that need to be animated on the y axis and then swap them out when animation complete, and remove Y rotation scene. It's not a pretty solution, but it's all there is until Apple adds better 2.5D capabilities.

    I've written a tutorial on SpriteKit and 2.5D with sample code if you need more info. http://www.sdkboy.com/?p=283

    self.rotAngle -= 10;
    
    float angle = (M_PI / 180.0f) * self.rotAngle/10;
    
    CATransform3D transform3DRotation = CATransform3DMakeRotation(1.0, angle, 0.0, 0.0);
    
    self.mySKView.layer.transform = transform3DRotation;
    

提交回复
热议问题