2013-03-06 85 views
0

我在Cocos2d iPhone游戏中使用CCSpeed行动。但是一直崩溃。Cocos2d 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。但没有提供代码解决方案。

回答

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. 
+0

加载后我们无法更改delayPerUnit。我需要随着游戏进程动态改变动画速度。 – Guru 2013-03-06 16:19:29

+1

该属性不是只读的,所以你可以在游戏中改变它。如果您担心并发(多个实例),请复制缓存的动画并更改副本上的delayPerUnit,然后运行副本。 – YvesLeBorg 2013-03-06 16:23:09

+0

其工作..感谢您的信息。 – Guru 2013-03-06 16:55:34

1

你做错了。 ;)

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

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

+0

没有...请给我一个代码示例(CCSpeed的用法)。在网上搜索但没有用。 – Guru 2013-03-07 04:50:23