25

I am making a game and I'm having troubles with rotating a sprite node, This is the code I have; What do I have to add to turn it, let's say 45 degrees?.

SKSpriteNode *platform = [SKSpriteNode spriteNodeWithImageNamed:@"YellowPlatform.png"];
platform.position = CGPointMake(CGRectGetMidX(self.frame), -200+CGRectGetMidY(self.frame));
platform.size = CGSizeMake(180, 10);
platform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:platform.size];
platform.physicsBody.dynamic = NO;
[self addChild:platform];
TheNeil
  • 3,321
  • 2
  • 27
  • 52
Vince
  • 648
  • 1
  • 7
  • 17
  • 3
    It was very simply, just add the following line of code: platform.zRotation = M_PI/4.0; bye – Vince Oct 15 '13 at 20:35

7 Answers7

75

Just do this without using actions:

sprite.zRotation = M_PI / 4.0f;

And in Swift 4:

sprite.zRotation = .pi / 4
kelin
  • 11,323
  • 6
  • 67
  • 104
quaertym
  • 3,917
  • 2
  • 29
  • 41
20

You make SKAction for the rotation and run that action on the node. for example:

//M_PI/4.0 is 45 degrees, you can make duration different from 0 if you want to show the rotation, if it is 0 it will rotate instantly
SKAction *rotation = [SKAction rotateByAngle: M_PI/4.0 duration:0]; 
//and just run the action
[yourNode runAction: rotation];  
BSevo
  • 743
  • 5
  • 13
15
sprite.zRotation = M_PI_4;

and

[sprite runAction:[SKAction rotateByAngle:M_PI_4 duration:0]];

are not the same.

running the SKAction will animate the change even if the duration is 0 it will do it over very short time. changing the zRotation will show it in the new rotation.

why this is important: if you add new sprite doing on it [SKAction rotateByAngle:] will create flickering/ugliness in the sprite as it comes on the view.

if the sprite is on the screen and you want to rotate it changing the zRotation will not be as nice as rotatebyAngle: even with 0 duration.

J. V.
  • 366
  • 3
  • 9
13

Using the predefined value will be faster:

sprite.zRotation = M_PI_4;

These predefined constants in math.h are available as literals and can be used to reduce actual processing of values like M_PI / 4.0

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • 2
    Nonsense. It will result in the same compiled code and take the same time (the difference will be immeasurable). Having said that, there is no reason not to use `M_PI_4`. – trojanfoe Apr 23 '15 at 10:36
  • Oh thanks for clearing this up. I did not know that M_PI/4 will be precompiled to reflect the same value as M_PI_4. – ZeMoon Apr 24 '15 at 09:49
7

In Swift 4 the way I got mine to rotate was

sprite.zRotation =  .pi / 2 // 90 degrees
sprite.zRotation =  .pi / 4 // 45 degrees
kelin
  • 11,323
  • 6
  • 67
  • 104
Dave Levy
  • 1,036
  • 13
  • 20
6

For SWIFT 3 / SWIFT 4 version you could use next:

sprite.zRotation = .pi / 4

or if you like actions:

let rotateAction = SKAction.rotate(toAngle: .pi / 4, duration: 0)
sprite.run(rotateAction)
kelin
  • 11,323
  • 6
  • 67
  • 104
wm.p1us
  • 2,019
  • 2
  • 27
  • 38
3
//rotates player node 90 degrees
player.zRotation = CGFloat(M_PI_2)*3;
//rotates 180 degrees
player.zRotation = CGFloat(M_PI_2)*2;
//rotates 270 degrees
player.zRotation = CGFloat(M_PI_2)*1;
//rotates 360 degrees (where it was originally)
player.zRotation = CGFloat(M_PI_2)*4;
//rotates 315 degrees
player.zRotation = (CGFloat(M_PI_2)*1)/2;
and so on...