Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate a sprite node in sprite kit?

Tags:

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]; 
like image 977
Vince Avatar asked Oct 15 '13 20:10

Vince


2 Answers

Just do this without using actions:

sprite.zRotation = M_PI / 4.0f; 

And in Swift 4:

sprite.zRotation = .pi / 4 
like image 94
quaertym Avatar answered Oct 06 '22 06:10

quaertym


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];   
like image 26
BSevo Avatar answered Oct 06 '22 06:10

BSevo