Changing parent-child structure at TMXLayer

Changing parent-child structure at TMXLayer
0.0 0

#1

I’m trying to changing parent-child structure in TMXLayer(SpriteBatchNode).
The code is below.


TMXTiledMap *tmxmap = TMXTiledMap::create("sample_map.tmx");
TMXLayer* ObjectsLayer = tmxmap->getLayer("LayerName");

Sprite* psprite = ObjectsLayer->getTileAt(Vec2(0, 0));
Sprite* psprite2 = ObjectsLayer->getTileAt(Vec2(0, 1));

ObjectsLayer->Node::removeChild(psprite, true);
psprite2->addChild(psprite);

Then, I got assert below.
Assert failed: updateQuadWithTexture: Invalid index
Assertion failed: (index >= 0 && index < _capacity), function updateQuad, file /Users/projectposition/cocos2d/cocos/renderer/CCTextureAtlas.cpp, line 317.

How can I do this.
Thanks.
-Hiroki


#3

I believe I reached to solution.
The source code should be below.

TMXTiledMap *tmxmap = TMXTiledMap::create("sample_map.tmx");
TMXLayer* ObjectsLayer = tmxmap->getLayer("LayerName");

Sprite* sprite_parent = ObjectsLayer->getTileAt(Vec2(0, 0));
Sprite* sprite_child = ObjectsLayer->getTileAt(Vec2(0, 1));

ObjectsLayer->removeChild(sprite_parent, true);
ObjectsLayer->removeChild(sprite_child, true);

sprite_parent->addChild(sprite_child);
ObjectsLayer->addChild(sprite_parent);

TMXTiledMap::addChild can add children recursively, if added node already have children.
So I had to remove parent from TMXLayer, then add child to parent, and add parent to TMXLayer again.
There could be more efficient way to implement, but this seems working correctly.

Thanks.