代码:
if (CGRectIntersectsRect(food.sprite.boundingBox, playerRect)) {
[food.sprite stopAllActions];
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"newpackman2.png", 0]]];
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"newpackman3.png", 1]]];
walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.2f];
self.walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
self.walkAction.tag = 1;
[chef runAction:_walkAction];
}
这段代码可以正常运行,在固定时间会触发动画。
但是我需要动画在3-4秒的延迟之后开始。
我用过 [sprite stopActionByTag:1] 没有实现。请高手帮忙,谢谢
你可以创建一个动作block来实现延迟,调用stopAllActions
:
CCSprite *sprite = ...;
CCCallBlock *block = [CCCallBlock actionWithBlock:^{
[sprite stopAllActions];
}];
然后使用CCDelayTime
:
CCDelayTime *time = [CCDelayTime actionWithDuration:4];
[sprite runAction:[CCSequence actions:time, block, nil]];
试试
CCDelayTime *time = [CCDelayTime actionWithDuration:10];// After 10 mile seconds
[sprite runAction:[CCSequence actions:time, block, nil]];