Cocos2d-x C++ Why does running an Action inside update() crashes the application?

Cocos2d-x v3
  • nightrobin
    Likes 0

    Problem Description

    Good day,

    The scenario is this, I have 3 sprites and I want to execute actions to them using sequence, inside the update().
    Another thing if the sprite is still executing the action, it will not acquire new action but if it is done executing the action it will create a new action then execute it.

    I used .isDone() for the checking.

     

    Here is the pseudo code:

    Legend for readability:
    SampleSprite - assuming that this is the sprite.
    isStillOnAction - boolean, true if currently executing action, false if not.
    RANDOM NUMBER FROM 2-5 - is a random number from 2 to 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;			
    			}
    	}
    }

    Result:
    It runs the sequence action but after that it freezes then it crashes.

    What is the possible problem that I overlooked?

    Thank you in advance :)


  • Sonar Systems admin
    Likes 0

    Have you tried without the for loop?

  • nightrobin
    Likes 0

    I tried to remove the for loop, but then it crahes immediately.

  • Sonar Systems admin
    Likes 0

    What does the console say when it crashes?

  • nightrobin
    Likes 0

    How can I output on the console, without using eclipse?

  • nightrobin
    Likes 0

    Thanks Sonar.

    I discovered that the cause of the problem is that I am running a repeatforever inside the sequence. 

    Only finitetimeaction or actions with definite time or repetition is allowed for sequences.

  • Sonar Systems admin
    Likes 0

    Cool      

Login to reply