1

I have a ball that start moving with an impulse but I want it to wait 3 seconds before doing so. I put this code thinking it would to something but it's not working.

    //add sprite to scene
[self addChild:ball];

SKAction *wait = [SKAction waitForDuration:3];

[self runAction:wait];

//create vector
CGVector myVector = CGVectorMake(10, 25);
//apply vector to ballphysics body
[ball.physicsBody applyImpulse:myVector];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217

1 Answers1

3

wait only applies to other actions.

If you want to apply your impulse after a wait you need to add that into a block as an action. once you have your wait action, and your applyImpulse action then we put these together into one sequence. Make sense?

    //add sprite to scene
    [self addChild:ball];

    SKAction *wait = [SKAction waitForDuration:3];

    SKAction *applyImpulse = [SKAction runBlock:^{
        //create vector
        CGVector myVector = CGVectorMake(10, 25);
        //apply vector to ballphysics body
        [ball.physicsBody applyImpulse:myVector];
    }];

    [self runAction:[SKAction sequence:@[
        wait,
        applyImpulse
    ]]];
hamobi
  • 7,940
  • 4
  • 35
  • 64