我陷入了奇怪的问题,我制作了一个box2d游戏,其中我的角色有不同的动画,用于行走,跳跃和站立。我在转换这些问题时遇到了问题。这是我的代码..
controlLayer.m
-(id)init{
self=[super init];
screenSize=[[CCDirector sharedDirector]winSize];
if(self!=nil){
[self initJoystickAndButtons];
[self scheduleUpdate];
return self;
}
}
-(void)applyJoystick:(SneakyJoystick *)aJoystick forTimeDelta:(float)deltaTime
{
if(aJoystick.velocity.x > 0.0f) {
[level1 walkBunny:(ccTime)aJoystick.velocity.x];
}
if(aJoystick.velocity.y > 0.0f) {
b2Vec2 force;
force.Set(0.0f, 150.0f);
level1.bunnyBody->ApplyLinearImpulse(force, level1.bunnyBody->GetWorldCenter());
[level1 jumpBunny];
}
if(aJoystick.velocity.x == 0.0f) {
[level1 standBunny];
}
}
}
你可以看到,我已经调用了我的角色的三个不同的状态,它在这里定义了动画,现在这里是那些方法...从我的更新方法调用applyJoystick。
level1Layer.m
-(void)walkBunny:(ccTime)duration{
NSLog(@"IN WALK BUNNY");
[animatingCyclist stopAllActions];
cyclistAnim = [CCAnimation animation];
[cyclistAnim addFrameWithFilename:@"walking-step-2.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-3.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-4.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-5.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-6.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-7.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-8.png"];
[cyclistAnim addFrameWithFilename:@"walking-step-9.png"];
cyclistAnimationAction = [CCAnimate actionWithDuration:(ccTime)duration animation:cyclistAnim restoreOriginalFrame:YES];
id repeatcyclistAnimation = [CCRepeatForever actionWithAction:cyclistAnimationAction];
[animatingCyclist runAction:repeatcyclistAnimation];
}
-(void)jumpBunny{
NSLog(@"IN JUMP BUNNY");
[animatingCyclist stopAllActions];
cyclistAnim = [CCAnimation animation];
[cyclistAnim addFrameWithFilename:@"jumping-step-1.png"];
[cyclistAnim addFrameWithFilename:@"jumping-step-2.png"];
[cyclistAnim addFrameWithFilename:@"jumping-step-3.png"];
[cyclistAnim addFrameWithFilename:@"jumping-step-4.png"];
[cyclistAnim addFrameWithFilename:@"jumping-step-5.png"];
[cyclistAnim addFrameWithFilename:@"jumping-step-6.png"];
[cyclistAnim addFrameWithFilename:@"jumping-step-7.png"];
cyclistAnimationAction = [CCAnimate actionWithDuration:5.5f animation:cyclistAnim restoreOriginalFrame:YES];
[animatingCyclist runAction:cyclistAnimationAction];
}
和standBunny相同......
问题是,我的这些方法在站立和行走时被反复调用,在动画的一次迭代之前再次调用它,所以我的动画没有完成,每次都会显示动画的第一张图像。希望你们都能解决我的问题! :(
答案 0 :(得分:0)
你正在停止动画,每一帧都重新创建它。例如:
if(aJoystick.velocity.x > 0.0f) {
[level1 walkBunny:(ccTime)aJoystick.velocity.x];
}
将其读作:“只要水平速度大于0,停止并重新启动步行兔子动画”。
您需要检查速度是否已从< = 0.0f更改为> 0.0f,然后只调用一次walkbunny方法。