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; 我的代碼有什麼問題? 謝謝。