代碼:
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]];