2012-02-17 127 views
1

我對動畫有一些疑問; 我有動畫課,更改動畫Cocos2d

@interface Player : CCNode{ 

    CCSprite *_player; 
    CCSpriteBatchNode *spriteSheet; 
    CCAction *walkAction; 
    CCAnimation *walkAnim; 


    int playerSpeed; 
    int xPos; 
    int yPos; 
    int state; 
    int currentAnim; 
} 
@property (nonatomic, retain) CCSprite *_player; 



-(id)init { 

    if((self=[super init])) { 

     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"duckAnimDown.plist"]; 
     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"DuckAnimTurn.plist"]; 
     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"duckAnimUp.plist"]; 



     currentAnim = 0; 
     state = 0; 
     [self chekState]; 
    } 
    return self; 
} 
-(void)setState:(int)st { 

    state = st; 
    [self chekState]; 
} 
-(void)chekState{ 


    [self stopAllActions]; 
    walkAnim = nil; 


    if (state == 1 && currentAnim != state) { 
     [spriteSheet removeChild:_player cleanup:YES]; 
     spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"duckAnimDown.png"]; 
     NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
     for (int i = 1; i <= 12 ; ++i) { 
      [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"duck_down%d.png", i]]]; 
     } 
     walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.04f];//0.06 
     _player = [CCSprite spriteWithSpriteFrameName:@"duck_down1.png"]; 
     walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]; 

    } 
    if (state == 2 && currentAnim != state) { 
     [spriteSheet removeChild:_player cleanup:YES]; 
     spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"DuckAnimTurn.png"]; 
     NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
     for (int i = 1; i <= 10 ; ++i) { 
      [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"duck_turn%d.png", i]]]; 
     } 
     walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.04f];//0.06 
     _player = [CCSprite spriteWithSpriteFrameName:@"duck_turn1.png"]; 
     walkAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]; 

    } 
    if (state == 3 && currentAnim != state) { 

     [spriteSheet removeChild:_player cleanup:YES]; 
     spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"duckAnimUp.png"]; 
     NSMutableArray *walkAnimFrames = [NSMutableArray array]; 

     for (int i = 1; i <= 13 ; ++i) { 
      [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"duck_up%d.png", i]]]; 
     } 
     walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.04f];//0.06 
     _player = [CCSprite spriteWithSpriteFrameName:@"duck_up1.png"]; 
     walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]; 

    } 
    if (currentAnim != state) { 
     [_player runAction:walkAction]; 
     [spriteSheet addChild:_player]; 
     [self addChild:spriteSheet]; 
     currentAnim = state; 
    } 



} 

我在GameplayScene中使用這個類;

duckSprite = [Player node]; 

要更改動畫我打電話:[duckSprite setState:2];

這沒關係,但是當我改變動畫FPS是非常低的20 - 25; 我的代碼有什麼問題? 謝謝。

回答

1

可能問題在於你在checkState方法中做了太多事情。 您幾乎可以將它全部移動到init方法,然後調用runAction來獲取適當的動畫。

另外,您應該只爲所有動畫使用一個精靈表。這樣,每次動畫更改時都不需要重新加載播放器精靈。

這是一個tutorial,它很好地解釋了一切。