CCSequence Stopped without error and crash

http://stackoverflow.com/questions/17205737/cocos2d-x-ccsequence-stopped-without-error-and-crash

I have posted this question sometimes before. It seems a normal usage but I believe this is a bad practice.
I would like to make sure if I am correct that if I call a sequence like this will cause the call stack accumulated?

Let me know if more information needed.

i am not sure :

in this function of obj::Callback, there is pScene->autoFill(),   and in HelloWorld, also has a function HelloWorld::autoFill(),  world you want call the function of HelloWorld::autoFill in the Obj::Callback function ?

Because i don’t know you want to do.

GameScene *pScene = dynamic_cast (this->getParent()->getParent());

    if (pScene)
    {
        if (!pScene->getIsPaused())
        {
            pScene->autoFill();
        }
    }

so i replaced the above code with CCLog(“call back.”) ;
and i got none error.

when i find two autoFill(), i have a guess: is you want call HelloWorld::autoFill in obj::callback? so i test this idea. and it’s also corect, but i add obj as a child of HelloWorld. so i just need getParent() once. i got HelloWorld layer.

HelloWorld::autoFill()
{
    // I skipped the object picking script here on purpose
    CCPoint pos = CCPointZero;
    obj->move(pos);
}

Obj::move(const CCPoint pos)
{
    CCPoint currentPos = this->getPosition();
    CCPoint moveDiff = ccpSub(currentPos, pos);
    float distanceToMove = ccpLength(moveDiff);
    float moveDuration = distanceToMove/ SPEED;

    CCFiniteTimeAction *pAction= CCMoveTo::create(moveDuration, pos);

    this->setZOrder(100);
    CCCallFunc *_callback = CCCallFunc::create(this, callfunc_selector(Obj::Callback));

    this->runAction(CCSequence::create( pAction, _callback, NULL ));
}

Obj::Callback()
{
    this->setZOrder(1);

    GameScene *pScene = dynamic_cast (this->getParent()->getParent());

    if (pScene)
    {
        if (!pScene->getIsPaused())
        {
            pScene->autoFill();
        }
    }
}

i am not sure :

in this function of obj::Callback, there is pScene->autoFill(),   and in HelloWorld, also has a function HelloWorld::autoFill(),  world you want call the function of HelloWorld::autoFill in the Obj::Callback function ?

Because i don’t know you want to do.

GameScene *pScene = dynamic_cast (this->getParent()->getParent());

    if (pScene)
    {
        if (!pScene->getIsPaused())
        {
            pScene->autoFill();
        }
    }

so i replaced the above code with CCLog(“call back.”) ;
and i got none error.

when i find two autoFill(), i have a guess: is you want call HelloWorld::autoFill in obj::callback? so i test this idea. and it’s also corect, but i add obj as a child of HelloWorld. so i just need getParent() once. i got HelloWorld layer.

HelloWorld::autoFill()
{
    // I skipped the object picking script here on purpose
    CCPoint pos = CCPointZero;
    obj->move(pos);
}

Obj::move(const CCPoint pos)
{
    CCPoint currentPos = this->getPosition();
    CCPoint moveDiff = ccpSub(currentPos, pos);
    float distanceToMove = ccpLength(moveDiff);
    float moveDuration = distanceToMove/ SPEED;

    CCFiniteTimeAction *pAction= CCMoveTo::create(moveDuration, pos);

    this->setZOrder(100);
    CCCallFunc *_callback = CCCallFunc::create(this, callfunc_selector(Obj::Callback));

    this->runAction(CCSequence::create( pAction, _callback, NULL ));
}

Obj::Callback()
{
    this->setZOrder(1);

    GameScene *pScene = dynamic_cast (this->getParent()->getParent());

    if (pScene)
    {
        if (!pScene->getIsPaused())
        {
            pScene->autoFill();
        }
    }
}

Sorry that I missed to point out that:

  1. Obj is added to a child layer in HelloWorld, so it is needed to getParent()->getParent() to get it
  2. I have not put the code of looping through a pool of obj (CCSprite). So after the first obj is called AutoFill, it will get off from the pool, then another CCSprite will call this instead.

Hope that is clear.

how the pool is created? which type of the pool?

in

HelloWorld::autoFill()
{
    // I skipped the object picking script here on purpose
    CCPoint pos = CCPointZero;
    obj->move(pos);
}

which type of obj?

if obj is an object of Obj. there is a problem: if you release it ,and another ccsprite also is obj??

Jonathan Yeung wrote:

Sorry that I missed to point out that:
>

  1. Obj is added to a child layer in HelloWorld, so it is needed to getParent()->getParent() to get it
  2. I have not put the code of looping through a pool of obj (CCSprite). So after the first obj is called AutoFill, it will get off from the pool, then another CCSprite will call this instead.
    >
    Hope that is clear.

The pool is just CCArray, or CCDictionary, just added into it
And all obj is the same which is a derived class of CCSprite

i’m not sure i know your intent.
i test the following steps:

1: new a class name Obj extends CCSprite. add move/callback function in Obj.
2: display an object of Obj, just test.
3: create std::list obj_list;
for (int i=0; isetPosition(getPos());
        layerObj->addChild(o);

        o->retain();
        obj_list.push_back(o);
    };
((Obj*)(obj_list.front()))->move(getPos());

void HelloWorld::autoFill()
{
    printf("count: %d.\n", count);
    count ++;
    // I skipped the object picking script here on purpose
    CCPoint pos = getPos();

    obj_list.pop_front();

    if(!obj_list.empty())
    {
        ((Obj*)(obj_list.front()))->move(getPos());
    }
}

CCPoint HelloWorld::getPos()
{
    float x,y;
    x = CCRANDOM_0_1() * winSize.width;
    y = CCRANDOM_0_1() * winSize.height;
    return ccp(x, y);
}

and i also got none error in ios or android.

Jonathan Yeung wrote:

The pool is just CCArray, or CCDictionary, just added into it
And all obj is the same which is a derived class of CCSprite

Please advise if I get it wrong.

The code here will lost one of the intention of the code, which is moving the object 1 by 1.
I believe looping in this way, all the object will be moving at the “same time” (sure they are not the same time but it will look like the same)

But thanks for the idea that I come up with an idea to have used scheduler to dispatch the object from time to time.

At this point I can narrow down the question:

  • Nested callback (the callback function will call the same function from the caller) is always not suggested?

I advise you test my solution, i got moving the object 1 by 1.
If there isn’t need to dispaly all these objs, we can hide others, and in callback, display the one that moves.

Nested callback is ok. but be careful memory leak.

Jonathan Yeung wrote:

Please advise if I get it wrong.
>
The code here will lost one of the intention of the code, which is moving the object 1 by 1.
I believe looping in this way, all the object will be moving at the “same time” (sure they are not the same time but it will look like the same)
>
But thanks for the idea that I come up with an idea to have used scheduler to dispatch the object from time to time.
>
At this point I can narrow down the question:

  • Nested callback (the callback function will call the same function from the caller) is always not suggested?

Thanks for the reply.

I will try it when I have time. But I have missed another point that occasionally the mentioned process has to be paused and resumed.
In this case, would you suggest to use scheduler instead?

oh, scheduler will be very convenient in this place. perhaps Random will be useful for occasion.

>

I will try it when I have time. But I have missed another point that occasionally the mentioned process has to be paused and resumed.
In this case, would you suggest to use scheduler instead?