【问题标题】:Cocos2d CCSpeed in action crashesCocos2d CCSpeed in action 崩溃
【发布时间】:2013-03-07 04:28:34
【问题描述】:

我在 cocos2d iPhone 游戏中使用了 CCSpeed 动作。但总是崩溃。

CCAnimation* animation = nil;
animation = [[CCAnimationCache sharedAnimationCache]  animationByName:ATTACK_ANIM];

if(animation)
{
    CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];

    id speed = [CCSpeed actionWithAction:animAction speed:0.13f];
    id calBlock = [CCCallBlock actionWithBlock:^{
                                                    //[self updateState:kTileState_Idle];
                                                }];
    CCSequence *sequence = [CCSequence actions:speed, calBlock, nil];        
    [self runAction:sequence];
}

但下面的代码工作正常..但无法改变动画速度。上面的代码有什么问题?

    CCAnimation* animation = nil;
    animation = [[CCAnimationCache sharedAnimationCache]  animationByName:quakeAnim];

    if(animation)
    {
        CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];
        id calBlock = [CCCallBlock actionWithBlock:^{
                                                        //[self updateState:kTileState_Idle];
                                                    }];
        CCSequence *sequence = [CCSequence actions:animAction, calBlock, nil];        
        [self runAction:sequence];
    }

这是一个花药thread。但没有提供代码解决方案。

【问题讨论】:

    标签: cocos2d-iphone ccspeed


    【解决方案1】:

    你能不能将 animation.delayPerUnit 设置为一些合适的值来改变它的执行速度?我通常在每次使用它之前计算该数字......因此我不需要在将动画放入缓存之前保留我设置的初始“delayPerUnit”。

    float totalAnimationTime = kSomeDesiredValue; // game logic dictates this, as well as
                                                  // rendering requirements (too slow will be perceived)
    animation.delayPerUnit = totalAnimationTime/animation.totalDelayUnits;
    
    // totalDelayUnits is a property of the animation, usually equal to the number
    // of frames.
    

    【讨论】:

    • delayPerUnit 加载后我们无法更改它。我需要随着游戏的进行动态地改变动画速度。
    • 该属性不是只读的,因此您可以在玩游戏时更改它。如果您担心并发(多个实例),请复制缓存动画并更改副本上的 delayPerUnit,然后运行副本。
    【解决方案2】:

    你做错了。 ;)

    CCSpeed 不能在序列中使用。您仍然需要将 animAction 添加到序列中,但保留对 CCSpeed 动作 (ivar) 的引用。然后,每当您想更改动画速度时,请更改 CCSpeed 实例的 speed 属性。

    使用 CCSped 需要您适当地管理序列的内存和 CCSpeed。

    【讨论】:

    • 没有得到..请给我一个代码示例(CCSpeed 的使用)。在网上搜索过,但没有用。
    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多