我有一些带动作的节点,如下所示:
SKSpriteNode *randomSprite = [SKSpriteNode spriteNodeWithImageNamed:spriteName];
SKAction *action = [SKAction moveToY:0 duration:3.5];
[randomSprite runAction:[SKAction repeatAction:action count:1]];
在某些代码处(不在我创建的第一个地方)我使用另一个动作创建新节点
SKSpriteNode *anotherSprite = [SKSpriteNode spriteNodeWithImageNamed:spriteName];
SKAction *action = [SKAction moveToY:0 duration:0.5];
[anotherSprite runAction:[SKAction repeatAction:action count:1]];
但是我需要在第一个完成他的动作时才开始第二个节点的动作我怎么做呢?
答案 0 :(得分:1)
你可以在另一个方法中使用第二个精灵,比如说
-(void)secondSpriteAction:(SKSpriteNode*)secondSprite{
SKAction *action = [SKAction moveToY:0 duration:0.5];
[secondSprite runAction:[SKAction repeatAction:action count:1]];
}
并在第一个精灵的动作
中为这个方法制作一个选择器SKAction *performSelector = [SKAction performSelector:@selector(secondSpriteAction:) onTarget:self];
然后你可以添加
SKAction *sequence = [SKAction sequence:@[action,performSelector]];
而不是重复动作,重复序列
[randomSprite runAction:[SKAction repeatAction:sequence count:1]];
这样的事情:
SKSpriteNode *randomSprite = [SKSpriteNode spriteNodeWithImageNamed:spriteName];
SKAction *action = [SKAction moveToY:0 duration:3.5];
SKAction *performSelector = [SKAction performSelector:@selector(secondSpriteAction:) onTarget:self];
SKAction *sequence = [SKAction sequence:@[action,performSelector]];
[randomSprite runAction:[SKAction repeatAction:sequence count:1]];
答案 1 :(得分:1)
您可以使用completion
runAction:
块
SKSpriteNode *randomSprite = [SKSpriteNode spriteNodeWithImageNamed:spriteName];
SKAction *action = [SKAction moveToY:0 duration:3.5];
[randomSprite runAction:[SKAction repeatAction:action count:1] completion:^{
SKSpriteNode *anotherSprite = [SKSpriteNode spriteNodeWithImageNamed:spriteName];
SKAction *action = [SKAction moveToY:0 duration:0.5];
[anotherSprite runAction:[SKAction repeatAction:action count:1]];
}];
如果你需要在代码中的其他位置初始化anotherSprite,你可以把它放在Lamikins建议的另一个方法中,并从完成块中调用该方法。
阅读runAction: completion:
方法here。