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。 私のコードで何が間違っていますか?おかげさまで