c++cocos2d-xcocos2d-x-3.0ccspriteccnode

setParent addChild does not seems to refresh children position in cocos2d-x 3.6


I want to change the parent of sprite, using oldparent->removeChild(child) from the old parent node, and then child->setParent(newparent) in the child sprite, passing the new parent as param. also tried with newparent->addChild(child).

The issue is that the position of the child does not change after that operation. I expect to see the child sprite changing its position to a new position relative to the parent transform. Is this the expected behavior or I need to call some update method to force a refresh of the child transform?


Solution

  • Last night I finally discover the problem. It happens after I decided to add a child->removeFromParentAndCleanup() before adding the child to a new parent. at this point, the program chrashed with no more clues in gdb. That make me suspect of a invalid memory issue.. (maybe the child was going erased in the background when it sees it is orphan?): so I try with:

      child->retain();
      child->removeFromParent();
      newparent->addChild(child);
      child->release();
    

    and both the crash and the problem with the update of the relative position had gone!