【问题标题】:Cocos2d: dealing with actions, animations, sequences of actions and CCTintByCocos2d:处理动作、动画、动作序列和 CCTintBy
【发布时间】:2012-12-16 14:34:22
【问题描述】:

我曾经在运行新操作时stopAllActions。我在使用 CCTintBy 动作时注意到一些问题,因为颜色正在逐渐添加并且对“reverse”的调用(参见 CCTintBy (CCActionInterval*) reverse 函数)是没有被调用(停止)。

这让我对我目前的方法和对 CCActions 的理解感到好奇。

例如。每当运行一个新动作时,我都会调用 [self stopAllActions] 但这并不适合 CCTintBy 动作的使用(它也会停止该动作并使精灵变为半色,并且底色会改变为相反函数不会被 stopAllActions 停止调用)。

我为您提供了我项目中最常见的操作。我在想而不是调用 stopAllActions,如果该操作尚未运行,则仅调用停止特定操作。这是一个好习惯吗?

 -(void) runExplosionAnimation
 {
[self stopAllActions];
//here should verify if the action is not already running and if so stop it
//To do so I should have a member variable like: CCAction * explosionAnim = [CCSequence actions: [CCAnimate actionWithDuration:0.4f animation:anim restoreOriginalFrame:false],  [CCHide action],  nil];
//Plus a boolean to distinguish if it is already running.. 

CCAnimation* anim = [[CCAnimationCache sharedAnimationCache] animationByName:@"bbb"];
if(anim!=nil){
    [self runAction:[CCSequence actions: [CCAnimate actionWithDuration:0.4f animation:anim restoreOriginalFrame:false],  [CCHide action],  nil]];
}
else{
    [self loadSharedAnimationIfNeeded];
}
}

使用布尔值确定 CCTintBy 操作是否仍在运行的一种方法是在再次调用 CCTintBy 操作之前手动恢复原始颜色。

-(void) gotHitWithFactor:(int)factor
    {
[self runGotHitAnimation];

self.hitPoints -= factor * 1;
if (self.hitPoints <= 0)
{
    isAlive=false;  
    [self runExplosionAnimation]; //Will also set enemy visibility to false
}
 }

-(void) runGotHitAnimation
{
    //hitAction is initialized as [CCTintBy actionWithDuration:0.1f red:100 green:100 blue:111];

    [self stopAction:hitAction];
    self.color = originalColour; //Where originalcolour is initialized as self.colour
    [self runAction:hitAction];
}

【问题讨论】:

    标签: ios animation cocos2d-iphone action


    【解决方案1】:

    你知道你可以标记动作,对吧?

    CCNode 类具有以下方法:

    stopActionByTag:
    getActionByTag:
    

    如果您运行一个新操作,您只需说明在每个特定情况下需要停止哪些其他操作。对于复杂的动作使用,我建议您在逻辑上将您的动作分为两组:游戏性和视觉效果。

    游戏动作是影响游戏玩法的所有动作,例如运动。视觉就是这样,视觉效果,如倾斜,旋转,着色。这种区别的关键在于它更容易确定行动的优先级。主要是,视觉动作不应停止、运行或以其他方式影响游戏动作。

    【讨论】:

    • 谢谢。我没有意识到这一点,因此答案以及将操作分成两个逻辑组的建议很有用。在停止之前了解某个操作是否正在运行是否有意义,是否有办法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多