Assert error message when adding CCTMXLayer

Hi all,

I’m currently develop a tile game using cocos2dx 2.1.4. Just now in the basic HelloWorldScene I added some code as follow:

// Adding tilemap.xml and extract tilemap background
tiledMap = CCTMXTiledMap::create(“TileGame/desert_map.tmx”);
mapScale = CCDirector::sharedDirector()>getWinSize.width / ).width;
//tiledMap
>setScale(mapScale);

// Get layers from the map
background = tiledMap~~>layerNamed;
background~~>setScale(mapScale);
this~~>addChild;
// Create an invisible meta-layer for collision detection
meta = tiledMap~~>layerNamed(“meta_layer”);
meta->setVisible(false);

CCDirector::sharedDirector()>setProjection;
CCObject * child = NULL;
CCARRAY_FOREACH, child) {
CCTMXLayer * c = dynamic_cast<CCTMXLayer *>;
c
>getTexture()>setAntiAliasTexParameters;
}
//this
>addChild(tiledMap);

And when my app run it show Error Dialog (mine ’s an Android apk). Follow the note I go to CCNode.cpp, line 507 and see this:

CCAssert( child->m_pParent == NULL, "child already added. It can't be added again");

I have no clue what ’s happening because it’s the first time I add a layer in my code. If I change to adding the whole CCTMXTiledMap, I run well (but how am I going to work with the meta-layer for collision detection later?).

Your guidance is appreciated. Thank you ^^

Umm, as I understand it, all the layers of a CCTMXTiledMap are added to be children of the tiled map by default. So this is what is making your program fail to assertion error:

background = tiledMap->layerNamed("background_layer");
this->addChild(background);

I guess I’m not sure what you problem would be to using your meta layer for collision detection even when it’s a child of tiledMap, the same as background, and you’ve added tiledMap to the scene. Wouldn’t it be handy that your background and meta have basically the same coordinates and scale (if you just scale and move the tiledMap when you need to do that)?

Sami Peltomaa wrote:

Umm, as I understand it, all the layers of a CCTMXTiledMap are added to be children of the tiled map by default. So this is what is making your program fail to assertion error:
[…]
>
I guess I’m not sure what you problem would be to using your meta layer for collision detection even when it’s a child of tiledMap, the same as background, and you’ve added tiledMap to the scene. Wouldn’t it be handy that your background and meta have basically the same coordinates and scale (if you just scale and move the tiledMap when you need to do that)?

Yeah maybe I’ve got it somehow. It’s all about API usage pattern…

I’m on the way now. Just need to add the whole map and using each layer of it later _