0

我陷入了奇怪的问题,我正在制作一款 box2d 游戏,其中我的角色具有不同的行走、跳跃和站立动画。我在切换这些时遇到了问题。这是我的代码..

控制层.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一样...

问题是,我的这些方法在站立和行走时被反复调用,在一次动画迭代之前它被再次调用,所以我的动画没有完成,它每次都会显示动画的第一张图像。希望大家能解决我的问题!:(

4

1 回答 1

0

您正在停止动画并在每一帧重新创建它。例如:

 if(aJoystick.velocity.x > 0.0f)  {            
            [level1 walkBunny:(ccTime)aJoystick.velocity.x];         
        } 

将其读作:“只要水平速度大于 0,停止并重新启动行走兔子动画”。

您需要检查速度是否已将状态从 <= 0.0f 更改为 > 0.0f,然后才调用 walkbunny 方法一次。

于 2013-01-11T14:24:43.453 回答