Home > other >  Tmx map dynamic block problem between items, troubled me for a long time.
Tmx map dynamic block problem between items, troubled me for a long time.

Time:11-21

The current scene, after loading a TMX map in the TMX map to join an elf, the following code
 
Auto map=cocos2d: : TMXTiledMap: : create (" 1. TMX ");
AddChild (map);
Auto _tamara=cocos2d: : Sprite: : create (" man. PNG ");

Auto foreLayer=map - & gt; GetLayer (" foreground ");
If (foreLayer)
{//was going to add the elves to map a call "foreground" layer, but one plus an error, do not know why, in desperation, had to be added to the map in the
//foreLayer - & gt; AddChild (_tamara);//complains here, don't add layer in
}
The map - & gt; AddChild (_tamara);


The 1. There is a "foreground" TMX map layer, I put a lot of trees, my idea is that when the Sprite moved to the front of the tree, elf tree, file moved to behind the tree, the tree file elves, elves will have interactive relationship with the layer,
So a logic and function,
Cocos built-in automatic block example I saw, is so,

Principle is: every tree, in a new layer, according to the order, if there are 10 rows of tree map, that is about to set up 10 layer to load the tree,
Then when the elf mobile update order, below is the official example code
 
Void TMXIsoZorder: : repositionSprite float (dt)
{
Auto p=_tamara - & gt; GetPosition ();
P=CC_POINT_POINTS_TO_PIXELS (p);
Auto map=getChildByTag (kTagTileMap);

//there are only 4 the layers. Three trees (grass and the layers)
//if tamara & lt; 30, z=4
//if tamara & lt; 60, z=3
//if tamara & lt; 90, z=2

4 - (int newZ=static_cast & lt; int> (p.y)/30);
NewZ=STD: : Max (newZ, 0);

The map - & gt; ReorderChild (_tamara newZ);
}

What row, relative set his order, in this way, the elves in the map the location of the lower, the greater the order,

It has reached the desired effect, but the question is: is too cumbersome, each row of trees must create a new layer, if all the trees in a layer, or elves all trees blocking, all elves, blocking or tree didn't developed branch to the expected effect, if the map is very big, there are 20 rows 30 trees, so it is too much trouble,
Is there a better way??

Cocos in another case, is by setting the elves Z attributes, as follows:

 
Void TMXOrthoVertexZ: : repositionSprite float (dt)
{
//tile height is 101 x81
//the map size: 12 x12
Auto p=_tamara - & gt; GetPosition ();
P=CC_POINT_POINTS_TO_PIXELS (p);
_tamara - & gt; SetPositionZ (- ((p.y + 81)/81));
}

His this case, all the tree can be in a layer, according to the spirit moves in the tree, dynamic adjusting this value, can realize the dynamic block effect,
But this example has a very important premise, that is the spirit must be exist in this layer, and this is very important!!!!!!
In my code, my spirit is extra added, add less than in the layer,
So this two ways, and for me is very uncomfortable, is there a compromise?

(as for why I want to be in the map dynamically add/delete this elf, actually I have a lot of map, the elves will exist in a map according to the circumstance, is the only elves, map is not the only)
  • Related