不断重复一个动作

时间:2014-10-07 03:33:37

标签: ios objective-c ios7 sprite-kit

我希望我的动作每2秒重复一次。这是我目前的代码:

    SKSpriteNode *lightnut = [SKSpriteNode spriteNodeWithImageNamed:@"lightnut.png"];
    BOOL heads = arc4random_uniform(100) < 50;
    lightnut.position = (heads)? CGPointMake(257,510) : CGPointMake(50,510);
    [self addChild: lightnut];

    SKAction *moveNodeUp = [SKAction moveByX:0.0 y:-600.0 duration:2.0];
    [lightnut runAction: moveNodeUp];

我如何一遍又一遍地重复上面代码中所说的动作?我希望精灵开始移动,等待2秒,然后再开始另一个。是否有可能在原始精灵完成移动到下一个点之前发送另一个精灵?

谢谢!

1 个答案:

答案 0 :(得分:0)

我建议您使用SKAction生成精灵,因为当您暂停/恢复场景或视图时,操作会暂停/恢复。以下是如何执行此操作的示例:

// Declare SKAction that waits 2 seconds
SKAction *wait = [SKAction waitForDuration:2.0];    

// Declare SKAction block to generate the sprites
SKAction *createSpriteBlock = [SKAction runBlock:^{
    SKSpriteNode *lightnut = [SKSpriteNode spriteNodeWithImageNamed:@"lightnut.png"];
    BOOL heads = arc4random_uniform(100) < 50;
    lightnut.position = (heads)? CGPointMake(257,510) : CGPointMake(50,510);
    [self addChild: lightnut];

    SKAction *moveNodeUp = [SKAction moveByX:0.0 y:-600.0 duration:2.0];
    [lightnut runAction: moveNodeUp];
}];

// Combine the actions
SKAction *waitThenRunBlock = [SKAction sequence:@[wait,createSpriteBlock]];

// Lather, rinse, repeat
[self runAction:[SKAction repeatActionForever:waitThenRunBlock]];