为什么在update()内运行Action会使应用程序崩溃?

时间:2016-03-20 23:14:42

标签: crash cocos2d-x cocos2d-x-3.0

美好的一天,

场景是这样的,我有3个精灵,我想在 update()中使用序列对它们执行操作。 另一件事如果精灵仍然在执行动作,它将不会获得新动作,但如果它完成了动作,它将创建一个新动作然后执行它。

我使用 .isDone()进行检查。

这是伪代码:

-Legend的可读性: SampleSprite - 假设这是精灵数组。 isStillOnAction - boolean, true 如果当前正在执行操作, false 则如果没有。 来自2-5的随机数 - 是2到5之间的随机数。

for(int ctr = 0; ctr < 3; ctr++){
if(!SpriteSample[ctr].isStillOnAction)          // not currently executing action, so we can now assign an action
{
    SampleSprite[ctr].isStillOnAction = true;   // the isStillOnAction boolean is changed to true

    if(SampleSprite[ctr].facingDirection == 1)
    {
        SampleSprite[ctr].moveAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 0, 100 ) );
        SampleSprite[ctr].stoppingAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 10, 0 ) );
    }
    else if(SampleSprite[ctr].facingDirection == 2)
    {
        SampleSprite[ctr].moveAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 0, -100 ) );
        SampleSprite[ctr].stoppingAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 10, 0 ) );
    }
    else if(SampleSprite[ctr].facingDirection == 3)
    {
        SampleSprite[ctr].moveAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( -100, 0 ) );
        SampleSprite[ctr].stoppingAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 0,10 ) );
    }
    else if(SampleSprite[ctr].facingDirection == 4)
    {
        SampleSprite[ctr].moveAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 100, 0 ) );
        SampleSprite[ctr].stoppingAction = cocos2d::MoveBy::create( RANDOM NUMBER FROM 2-5, cocos2d::Vec2( 0, 10 ) );
    }

    SampleSprite[ctr].moveStopAction = cocos2d::Sequence::create(SampleSprite[ctr].moveAction, SampleSprite[ctr].stoppingAction, nullptr);
    SampleSprite[ctr].characterSprite->runAction( SampleSprite[ctr].moveStopAction );
}
else    // currently executing action
{
        if(SampleSprite[ctr].moveStopAction->isDone())          // check if done executing action
        {
            SampleSprite[ctr].isStillOnAction = false;          
        }
}}

结果: 它运行序列操作,但之后冻结然后崩溃

我忽略了可能出现的问题是什么?

提前谢谢你的微笑

1 个答案:

答案 0 :(得分:0)

在计划的功能中运行操作不会使应用程序崩溃。问题可能是:

  1. 当您使用.isDone()时,该动作已被释放。当一个动作完成时,它将减少它的引用计数并被释放,除非你保留它(不推荐)。
  2. MoveBy::create()会返回一个自动释放对象,这意味着如果您没有在其上调用runAction或保留它,它将在此帧结束时释放。
  3. 您可以使用标签检查某项操作是否正在运行:

    static const int MY_MOVEBY_ACTION = 3333;
    for(int ctr = 0; ctr < 3; ctr++){
    if(!SpriteSample[ctr].characterSprite->getActionByTag(MY_MOVE_ACTION))          // not currently executing action, so we can now assign an action
    {
        SampleSprite[ctr].isStillOnAction = true;   // the isStillOnAction boolean is changed to true
    
        //......
    
        SampleSprite[ctr].moveStopAction = cocos2d::Sequence::create(SampleSprite[ctr].moveAction, SampleSprite[ctr].stoppingAction, nullptr);
        moveStopAction->setTag(MY_MOVE_ACTION);
        SampleSprite[ctr].characterSprite->runAction( SampleSprite[ctr].moveStopAction );
    }
    else    // currently executing action
    {
            if(SampleSprite[ctr].moveStopAction->isDone())          // check if done executing action
            {
                SampleSprite[ctr].isStillOnAction = false;          
            }
    }}