Search code examples
if-statementsprite-kitskaction

Do a Command After an Action Has Been Completed - SpriteKit


In SpriteKit, I have a simple action for my sprite:

sprite.size = CGSizeMake(40, 10);
sprite.physicsBody =[SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite.position = CGPointMake(65, 230);
SKAction *changeImage = [SKAction setTexture:[SKTexture textureWithImageNamed:@"raniSlide"]];
SKAction *wait = [SKAction waitForDuration:.1];
SKAction *changeImage2 = [SKAction setTexture:[SKTexture textureWithImageNamed:@"raniStick"]];
SKAction *sequence=[SKAction sequence:@[changeImage, wait, changeImage2]];
[sprite runAction:sequence];

It changes textures, waits, then changes textures again. This is so it ducks for a certain time period. After ducking I want it to change back to its original size. When I put the code for changing size after the runAction command, it doesn't perform the first sprite.size = CGSizeMake(40, 10), and doesn't duck at all, as it stays the same size as it was in the beginning. So I was wondering how to make an if statement that when if an action has been completed, it then changes the size of the sprite.


Solution

  • You can't do it with an if statement, but take a look at runAction:completion:. You use it like:

    [sprite runAction:sequence completion:^{
        // the action is complete, change size back
    }];