0

我一生都无法弄清楚为什么会发生这种情况。我有一个从 CCLayer 派生的类。我在初始化类时像这样安排方法调用

//create an update method for keeping track of how long its been since an animation has played
    [self schedule:@selector(playIdleAnimation:)];

方法是

//an update method that will play an idle animation after a random period of idleness
-(void) playIdleAnimation:(ccTime) dt {

//if the user isn't playing an animation, increment the time since last animation variable
if ([bodySprite numberOfRunningActions] == 0) {

    timeSinceLastAnimation += (float)dt;

    //now check to see if we have surpassed the time set to cause an idle animation
    if (timeSinceLastAnimation > (arc4random() %14) + 8) {

        //reset the cooldown timer
        timeSinceLastAnimation = 0;

        [bodySprite stopAllActions];

        //play the idle animation
        //[bodySprite runAction:[CCAnimate actionWithAnimation:waitAnimation restoreOriginalFrame:NO]]; 

        NSLog(@"PLAYING IDLE ANIMATION");
                }
}
//player is currently playing animation so reset the time since last animation
else
    timeSinceLastAnimation = 0;

}

但是,当我运行程序时,控制台语句显示每个冷却时间都通过了两次条件

012-06-29 09:52:57.667 测试游戏[5193:707] 播放空闲动画

2012-06-29 09:52:57.701 测试游戏[5193:707] 播放空闲动画

2012-06-29 09:53:05.750 测试游戏[5193:707] 播放空闲动画

2012-06-29 09:53:05.851 测试游戏[5193:707] 播放空闲动画

我正在尝试修复当我播放完空闲动画时游戏崩溃的错误,我确信这与它有关。

4

1 回答 1

0

我看不到您在哪里取消计划选择器。我敢打赌,在帧启动时被调用是正常行为,您会看到它被触发两次,因为释放层需要一个帧。

如果您想要一次性方法调用,请执行以下操作:

-(void) playIdleAnimation:(ccTime) dt {
    [self unschedule:_cmd];

    // rest of the code here
}

Cocos2d 2.0 有一个 scheduleOnce 方法,您可以使用它。

于 2012-06-29T11:18:22.273 回答