我目前正在开发一款带有Kobold2d(cocos2d)的手机游戏,但我在使用多个动画制作一个CCSprite时遇到了一些麻烦。我的问题是:使用相同的CCSprite(播放器)如何使用两个不同的动画为其设置动画。我正在使用Kobold2D 2.0.4和Xcode 4.6,对于动画,我有一个用pvr.ccz压缩的textureAtlas。
我创建了一个动画助手类:
+(CCAnimation *) startFrames:(int) startFrame
endFrame:(int) endFrame
frameName:(NSString *) frameName
delay:(float) delay
{
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:endFrame];
CCSpriteFrameCache *sharedFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
for (int i = startFrame; i <= endFrame; i++) {
CCSpriteFrame *frame = [sharedFrameCache spriteFrameByName:[NSString stringWithFormat:@"%@%i.png",frameName,i]];
[frames addObject:frame];
}
return [CCAnimation animationWithSpriteFrames:frames delay:delay];
}
+(CCAnimation *) jumpPoints:(CGPoint[]) points
startFrames:(int) startFrame
endFrame:(int) endFrame
frameName:(NSString *) frameName
delay:(float) delay
{
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:endFrame];
CCSpriteFrameCache *sharedFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
for (int i = startFrame; i <= endFrame; i++) {
CCSpriteFrame *frame = [sharedFrameCache spriteFrameByName:[NSString stringWithFormat:@"%@%i.png",frameName,i]];
[frame setOffsetInPixels:points[i]];
[frames addObject:frame];
}
return [CCAnimation animationWithSpriteFrames:frames delay:delay];
}
我有一个播放器类,我初始化播放器并创建两个CCAnimation。在头文件中,我存储了两个CCAnimate类型变量:
CCAnimate *jump;
CCAnimate *walk;
在.m文件中
-(id) init
{
if (self = [super init])
{
sprite = [[Loading shareLoading] loadTempCcsprite];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
localPostion = CGPointMake(screenSize.width, screenSize.height);
CGPoint points[] = {CGPointMake(0, 10),CGPointMake(0, 50),CGPointMake(0, 80),
CGPointMake(0, 10),CGPointMake(0, 0)};
CCAnimation *animationJump = [CCAnimationHelper jumpPoints:points startFrames:0 endFrame:4 frameName:@"cactus" delay:0.10f];
jump = [CCAnimate actionWithAnimation:animationJump];
CCAnimation *animationWalk = [CCAnimationHelper startFrames:0 endFrame:4 frameName:@"cactus" delay:0.10f];
walk = [CCAnimate actionWithAnimation:animationWalk];
[self addChild:sprite];
}
return self;
}
最后,我有输入层,用户有两个跳跃按钮和攻击和操纵杆移动播放器。
以下是代码:
-(void) update:(ccTime)delta
{
if ([jump active])
{
CCAnimate *animation = [player jump];
[player runAction:animation];
}
else if (joystick.velocity.x > 0)
{
float velocity = [joystick velocity].x * 700 * delta;
CGPoint playerPosition = CGPointMake([player localPostion].x + velocity * delta, [player localPostion].y );
id animation = [player walk];
[player runAction:animation];
[player setLocalPostion:playerPosition];
}
}
如果我编译此代码,在输入图层中按下跳转按钮时游戏崩溃并且输出控制台说:
* 断言失败 - [CCActionManager addAction:target:paused:]
答案 0 :(得分:1)
您正在尝试在它仍在运行时再次运行相同的操作。
要避免这种情况,请始终在runAction:
之前调用stopAction[player stopAction:animation];
[player runAction:animation];