2011-06-21 80 views
1

請讓我在編寫這段代碼時需要幫助。我打算做一個動畫,在xcode中使用cocos2d和box2d添加一個精靈。但由於一些奇怪的原因,我無法讓精靈重複動畫。在cocos2d,box2d和Xcode中爲動畫製作精靈

該代碼可以成功構建,但只能動畫一次。任何人都可以幫助並告訴我我做得不對嗎?

實現文件如下:#進口 「Mosquito.h」 #進口 「Box2DHelpers.h」

@implementation Mosquito 

@synthesize flyingAnim; 

- (void) dealloc{ 
[flyingAnim release]; 

[super dealloc]; 
} 




-(void)initAnimations { 

    flyingAnim = [self loadPlistForAnimationWithName:@"flyingAnim" 
    andClassName:NSStringFromClass([self class])]; 
[[CCAnimationCache sharedAnimationCache] addAnimation:flyingAnim 
      name:@"flyingAnim"]; 

      } 

    -(void)changeState:(CharacterStates)newState { 
    [self stopAllActions]; 
    id action = nil; 
    // id flyingAction = nil; 
    //CGPoint newPosition; 
    [self setCharacterState:newState]; 
    switch (newState) { 

      case kStateIdle: 
       [self setDisplayFrame: 
       [[CCSpriteFrameCache sharedSpriteFrameCache] 
        spriteFrameByName:@"Mosquito_anim_1.png"]]; 
       break; 
    case kStateFlying: 

     action = [CCAnimate actionWithAnimation:flyingAnim 
          restoreOriginalFrame:NO]; 
     break; 

    case kStateTakingDamage: 
     action = [CCBlink actionWithDuration:1.0 blinks:3.0]; 
     break; 

    default: 
      //CCLOG(@"Unhandled state %d in Mosquito", newState); 
     break; 
    } 
    if (action != nil) { 
    [self runAction:action]; 
    } 
    } 




- (id)initWithWorld:(b2World *)theWorld atLocation:(CGPoint)location { 
if ((self = [super init])) { 
    world = theWorld; 
    [self setDisplayFrame:[[CCSpriteFrameCache 
          sharedSpriteFrameCache] 
    spriteFrameByName:@"Mosquito_anim_1.png"]]; 
    gameObjectType = kMosquitoType; 
    characterHealth = 100.0f; 
    [self createBodyAtLocation:location]; 
    [self initAnimations]; 
} 
return self; 
} 


- (void) updateStateWithDeltaTime:(ccTime)deltaTime 
     andListOfGameObjects:(CCArray *)listOfGameObjects { 
    //CGPoint oldPosition = self.position; 



if ((characterState == kStateDestroyed) && 
    ([self numberOfRunningActions] > 0)) { 
    return; 
} 
if (characterState != kStateFlying && 
    [self numberOfRunningActions] == 0) { 
    [self changeState:kStateFlying]; 
} 


    } 
    @end 

感謝。

回答

0
id repeatAnimation = [CCRepeatForever actionWithAction:action]; 

永遠重複,你需要做的,否則,你需要做的只是:

[self runAction:action]; 

一次。

而且,你可能要考慮不重新分配的行動CCBlink再拍行動,並呼籲

[self stopAllActions]; 
id blinkAction = [CCBlink actionWithDuration:1.0 blinks:3.0]; 
[self runAction:blinkAction]; 
+0

謝謝你。我會嘗試你的建議,看看會發生什麼。再次感謝。 – Zaki

+0

感謝百萬!你的建議工作...是的,它的工作。我很感激你的幫助。再次感謝。 – Zaki