2012-03-03 88 views
1

所以這裏的問題是當我創建一個動畫射彈時,一切都很好。只要用戶創建第二個,第一個停止動畫。Cocos2D - 當我動畫第二個精靈時,首先停止動畫

好了,這裏就是我如何設置它在我的初始化:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: 
    @"acorns.plist"]; 

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode 
             batchNodeWithFile:@"acorns.png"]; 
[self addChild:spriteSheet]; 

NSMutableArray *flyingFrames = [NSMutableArray array]; 
    for(int i = 1; i <= 4; ++i) { 
     [flyingFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"acorn%d.png", i]]]; 
    } 

CCAnimation *flying = [CCAnimation 
           animationWithFrames:flyingFrames delay:0.5f]; 

self.flying = [CCRepeatForever actionWithAction: 
        [CCAnimate actionWithAnimation:flying restoreOriginalFrame:NO]]; 

然後,我創建子彈的方法被稱爲當用戶點擊屏幕,我做的:

CCSprite *bullet = [CCSprite spriteWithSpriteFrameName:@"acorn1.png"]; 
bullet.position = CGPointMake(140.0f, FLOOR_HEIGHT+145.0f); 
[bullet runAction:_flying]; 

[self addChild:bullet z:9]; 
[bullets addObject:bullet]; 

因此,用戶第一次點擊時,一切正常。第二次,創建了動畫子彈,但現有的子彈停止動畫,等等。

回答

2

我相信每個精靈都應該有自己的行爲,也就是說,當行動正在進行時,你不能重用一個行動。例如:

CCSprite *bullet = [CCSprite spriteWithSpriteFrameName:@"acorn1.png"]; 
bullet.position = CGPointMake(140.0f, FLOOR_HEIGHT+145.0f); 
id _fly=[CCAnimation animationWithFrames:flyingFrames delay:0.5f]; 
id _flyForever = [[CCRepeatForever _fly]; 
[bullet runAction:_flyForever]; 
[self addChild:bullet z:9]; 
[bullets addObject:buller]; 

flyingFrames可以被多個動畫引用,所以您必須注意保留數組以供重用。