setParent addChild 似乎没有刷新 children 在 cocos2d-x 3.6 中的位置
setParent addChild does not seems to refresh children position in cocos2d-x 3.6
我想改变精灵的parent,使用旧parent节点的oldparent->removeChild(child),然后child->setParent(newparent) 在 child 精灵中,传递新的 parent 作为参数。还尝试使用 newparent->addChild(child)。
问题是 child 的位置在该操作后没有改变。我希望看到 child 精灵将其位置更改为相对于 parent 变换的新位置。
这是预期的行为还是我需要调用一些更新方法来强制刷新 child 转换?
昨天晚上终于发现问题了。它发生在我决定在将 child 添加到新的 parent 之前添加 child->removeFromParentAndCleanup() 之后。
此时,程序崩溃了,gdb 中没有更多线索。
这让我怀疑是一个无效的内存问题..(也许 child 在看到它是孤儿时在后台被删除了?):所以我尝试使用:
child->retain();
child->removeFromParent();
newparent->addChild(child);
child->release();
崩溃和更新相对位置的问题都解决了!
我想改变精灵的parent,使用旧parent节点的oldparent->removeChild(child),然后child->setParent(newparent) 在 child 精灵中,传递新的 parent 作为参数。还尝试使用 newparent->addChild(child)。
问题是 child 的位置在该操作后没有改变。我希望看到 child 精灵将其位置更改为相对于 parent 变换的新位置。 这是预期的行为还是我需要调用一些更新方法来强制刷新 child 转换?
昨天晚上终于发现问题了。它发生在我决定在将 child 添加到新的 parent 之前添加 child->removeFromParentAndCleanup() 之后。 此时,程序崩溃了,gdb 中没有更多线索。 这让我怀疑是一个无效的内存问题..(也许 child 在看到它是孤儿时在后台被删除了?):所以我尝试使用:
child->retain();
child->removeFromParent();
newparent->addChild(child);
child->release();
崩溃和更新相对位置的问题都解决了!