Search code examples
cocos2d-xframe-rate

Remove child from Batch node one after another with a pause


I am trying to remove a set of children from the batch node one by one after some pause . So if three children are there, then firstly parent will disappear immidiatley , then after 1 second first child will disappear, then after another 1 second(total 2 seconds) 2nd child will disappear and after another 1 second(total 3 seconds) third child will disappear.

Right now I am removing them from the batchnode like :-

batchNode->removeChild(child1,true);
sleep(1);
batchNode->removeChild(child2,true);
sleep(2)
batchNode->removeChild(child3,true);

But they all disappear from screen at same time ! although pause is there. Is it because they all are part of same batchNode so any action taken on children will be applied at one go. ?

Please share your thoughts


Solution

  • I am able to do it , I used two different ways as described:-

    1> By postponing the removal of subsequent children in further frames.

    2> or instead of depending on the frames, I used delta time (time since the last frame was drawn).So remove a child only after say 0.5 secs.

    Using either of the above way , I can omit the use of sleep which can hog my main thread.

    Here are the details, considering I have in total three children to remove:-

    1> By postponing the removal of subsequent children in further frames.

    In the current frame I will remove first child then will set a flag to notify in the Update not to do anything but wait for say 15 frames, while waiting 15 frames I can do other tasks also which doesnot have impact on gameplay

    Although with this I have flexibility to wait for any number of frames, but I will have to regulate the frame rate for different devices, as in slow devices, waiting 15 frames can take more time than fast device, and I am not able to know if cocos2dx does regulate the frame rate itself, so I quickly jumped to next solution.

    2> or instead of depending on the frames, I used delta time (time since the last frame was drawn).So remove a child only after say 0.5 secs.

    Here instead of waiting 15 frames I used following condition inside Update(float dt):-

    if(gameState==WaitForNextChildRemoval)
    {
    deltaTime=deltaTime+dt;
    
    if(deltaTime>=0.5)
    {
    releaseNextChild();
    deltaTime=0;
    }
    }
    

    here irrespective of the device(slow or fast), next child will be removed after 0.5 secs. One improvement I think should be made is when deltaTime becomes more than say 1 sec , and then instead of removing only next one child, it should remove two children (0.5 secs for each) at one go , as player is expecting to remove one child after 0.5 secs so deltaTime being 1 sec(approx), I should balance this expectation.

    Please share your thoughts.