guruD
guruD

Reputation: 7

Parallax effect on tile map layers

i am using three layers of tiled map and i want to give parallax effect on these layers. my code is:

     CCTMXTiledMap *city = CCTMXTiledMap::create("City.tmx");

     CCTMXLayer* ForegroundLayer = city->layerNamed("ForeGround");
 CCTMXLayer* BackgroundLayer1 = city->layerNamed("Background1");
 CCTMXLayer* BackgroundLayer2 = city->layerNamed("Background2");

     CCParallaxNode* voidNode = CCParallaxNode::create();

    // NOW add the 3 layers to the 'void' node

     voidNode->addChild(BackgroundLayer2, -1, ccp(0.4f,0.5f), CCPointZero);
     voidNode->addChild(BackgroundLayer1, 1, ccp(2.2f,1.0f), ccp(0,-200) );
     voidNode->addChild(ForegroundLayer, 2, ccp(3.0f,2.5f), ccp(200,800) );

voidNode->runAction(temp); //some action temp

    addChild(voidNode);

It gives assertion failed: child->m_pParent==0 Same code works if we use sprites instead of TMXLayers. what I have done wrong in this code?

Upvotes: 0

Views: 1440

Answers (2)

try this code

backgroundLayer->retain();
backgroundLayer->removeFromParentAndCleanup(false);
parallaxNode->addChild(backroundLayer, 0, Vec2(0, 0), Vec2(0, 0));//some points
backgroundLayer->release();

Upvotes: 0

CodeSmile
CodeSmile

Reputation: 64478

The layers are already child nodes of the CCTMXTiledMap. A node can only have one parent.

You can try removing each layer from its parent first, the add them to voidnode. However chances are this won't work because the layers may depend on their tilemap parent.

Upvotes: 1

Related Questions