buczek
buczek

Reputation: 2050

iOS 7 Sprite Kit speed change for animation

I have a runAction which is animating a SKSpriteNode. I have the node moving up and down in a repeatActionForever. I would like the node to slow down as the node is moving up and speed up as the node is moving down.

[node runAction:[SKAction repeatActionForever:
                       [SKAction sequence:@
                        [[SKAction speedTo:0.1 duration:0.5],
                         [SKAction moveToY:2 * node.size.height / 3 duration:0.5],
                         [SKAction speedTo:1 duration:0.5],
                         [SKAction moveToY:node.size.height / 2 duration:0.5],
                         [SKAction moveToY:node.size.height duration:1],
                         [SKAction moveToY:node.size.height / 2 duration:1]]]]];

When I add the line [SKAction speedTo:0 duration:0.5], the rest of the code is run at speed of 0 after the 0.5 seconds even though I added a second speedTo action which would increase the speed to 1.

The problem: How do I change the speed of a node as the node is moving rather than having a stagnant speed for each direction.

Thanks in advance.

Upvotes: 2

Views: 2574

Answers (1)

ZeMoon
ZeMoon

Reputation: 20284

Look up the various types of SKActionTimingMode and apply the ones as needed to your situation. This will remove the need for anything like [SKAction speedTo:0.1 duration:0.5].

https://developer.apple.com/library/mac/documentation/SpriteKit/Reference/SKAction_Ref/Reference/Reference.html#//apple_ref/c/tdef/SKActionTimingMode

You can use the SKActionTimingEaseOut for the action that makes the node move up and SKActionTimingEaseIn for the action that makes the node move down.

SKAction *actionMoveUp = [SKAction moveToY:2 * node.size.height / 3 duration:0.5];
actionMoveUp.timingMode = SKActionTimingEaseOut;

SKAction *actionMoveDown = [SKAction moveToY:node.size.height / 2 duration:0.5];
actionMoveDown.timingMode = SKActionTimingEaseIn;

SKAction *actionMoveUpHalf = [SKAction moveToY:node.size.height duration:1];
actionMoveUp.timingMode = SKActionTimingEaseOut;

[node runAction:[SKAction repeatActionForever:
                       [SKAction sequence:@
                        [actionMoveUp,
                         actionMoveDown,
                         actionMoveUpHalf,
                         actionMoveDown]]]];

Upvotes: 6

Related Questions