Cocos2d - 基于触摸改变精灵动画(精灵表)
问题描述:
我想我主要是在对我使用我的Player类的方式进行批评之后,以及如何改进它。Cocos2d - 基于触摸改变精灵动画(精灵表)
我的spritesheet包含了所有我觉得我需要的动作的顺序。但我最初想要的是,当我按下“跳转”按钮时,该角色正在经历的动画停止,并且使用它所使用的spritesheet中的“跳转”序列。 (走路 - 例如第7帧到第9帧,其中跳帧从第10帧到第13帧)。
这里是我生产我的球员精灵:
Player.m:
-(id)playerCharacter {
if (self = [super init]) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Soldier.plist"];
// Create a sprite sheet with the Player images
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Soldier.png"];
[self addChild:spriteSheet z:15];
NSMutableArray *walking = [NSMutableArray array];
for(int i = 7; i <= 9; ++i) {
[walking addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]];
}
CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walking delay:0.2f];
_walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkingAnimation]];
walkAnim.restoreOriginalFrame = NO;
[self runAction:_walkAction];
self.velocity = ccp(0.0, 0.0);
}
return self;
}
GameLevelLayer.m
player = [[Player alloc] playerCharacter];
player.position = ccp(100,50);
[map addChild:player z:15];
我想我的问题是.. 我应该动该方法的整个CCAnimation组件超出了那里,进入一个新方法,该方法检查一系列定义spritesheet /动画序列的BOOL?
我是否甚至以最有效的方式使用上述代码?虽然我明白它的工作原理......我宁愿了解我是否正确地做事情,而不是继续下去,这会增加我的项目效率。
我非常感谢任何反馈和帮助。
答
您没有正确使用CCSpriteBatchNode。把它想象成一个可以从一个精灵表单中保存所有精灵的图层。你的球员应该被添加到batchnode,并且你的batchnode被添加到你的游戏层。
-(id)playerCharacterOnSheet:(CCSpriteBatchNode*)batchNode
{
// this is assuming Player is a CCSprite subclass
// set this to the "static"/default player sprite image
if (self = [super initWithSpriteFrameName:@"Soldier1.png"])
{
// Load self to the batchnode
[batchNode addChild:self];
NSMutableArray *walking = [NSMutableArray array];
for(int i = 7; i <= 9; ++i) {
[walking addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]];
CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walking delay:0.2f];
_walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkingAnimation]];
walkAnim.restoreOriginalFrame = NO;
[self runAction:_walkAction];
self.velocity = ccp(0.0, 0.0);
}
return self;
}
然后在GameLevelLayer,创建batchNode和球员:那么如下的球员应该被创建
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Soldier.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Soldier.png"];
[map addChild:spriteSheet];
player = [[Player alloc] playerCharacterOnSheet:spriteSheet];
player.position = ccp(100,50);
要执行其他操作,只需添加方法,您的播放器类来执行他们(停止任何其他正在运行的动作)例如:
-(void)jumpAction
{
[self stopAllActions];
NSMutableArray *jumping = [NSMutableArray array];
for(int i = 10; i <= 13; ++i) {
[jumping addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]];
}
CCAnimation *jumpingAnimation = [CCAnimation animationWithSpriteFrames:jumping delay:0.2f];
CCAction *jumpAction = [CCAnimate actionWithAnimation:jumpingAnimation ];
[self runAction:jumpAction];
}
要执行,呼叫:
[player jumpAction];