문제

I have a 50 X 50 isometric Tiled Map with base tile : 64 X 32.

I am using this function to create a sprite and add to a particular tile dynamically.

-(void)addTile:(NSString *)tileName AtPos:(CGPoint)tilePos onTileMap:(CCTMXTiledMap *)tileMap
{
  CCTMXLayer *floorLayer=[tileMap layerNamed:@"FloorLayer"];
  NSAssert(floorLayer !=nil, @"Ground layer not found!");

  CGPoint tilePositionOnMap = [floorLayer positionAt:tilePos];


    CCSprite *addedTile = [[CCSprite alloc] initWithFile:tileName];
    addedTile.anchorPoint = CGPointMake(0, 0);
    addedTile.position = tilePositionOnMap;

    addedTile.vertexZ = [self calculateVertexZ:tilePos tileMap:tileMap];

    [tileMap addChild:addedTile];
}

Floor layer is the only layer in my Tiled map and I have added the property cc_vertexz = -1000 to this layer.

I took the calculateVertexZ method from the KnightFight project. Based on the tile coordinates on isometric map it'll calculate the vertexZ and once you see the map it seems to make sense too.

-(float) calculateVertexZ:(CGPoint)tilePos tileMap:(CCTMXTiledMap*)tileMap
{
    float lowestZ = -(tileMap.mapSize.width + tileMap.mapSize.height);
    float currentZ = tilePos.x + tilePos.y;
    return (lowestZ + currentZ + 1);
}

Now this is the code which i'm writing in the -init of my HelloWorldLayer of template cocos2d-2 project. -

self.myTileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"IsometricMap.tmx"];
[self addChild:self.myTileMap z:-100 tag:TileMapNode];

[self addTile:@"walls-02.png" AtPos:CGPointMake(0, 0) onTileMap:self.myTileMap];
[self addTile:@"walls-02.png" AtPos:CGPointMake(0, 1) onTileMap:self.myTileMap];

[self addTile:@"walls-02.png" AtPos:CGPointMake(4, 1) onTileMap:self.myTileMap];
[self addTile:@"walls-02.png" AtPos:CGPointMake(4, 0) onTileMap:self.myTileMap];

Here's the wall image - enter image description here

And here's the issue - enter image description here

Case 1 (0,0) should be behind (0,1) according to calculateVertexZ method. And hence the sprite on ( (0,1) is rendered OVER sprite on (0,0).

Case 2 (4,0) should be behind (4,1) according to calculateVertexZ method. But somehow, because I'm adding block on (4,0) AFTER (4,1) its not giving me the desired results.

I had read that when two sprites have same vertexZ ONLY then whichever sprite was added later will be on top. But here the sprites have different vertexZ values, still order of creation is overriding that.

Also, I can't figure out what to do with zorder in this equation. SOMEBODY PLS HELP

도움이 되었습니까?

해결책

I solved this by using the vertexZ property and zOrder property of the base tile.

-(void)addTile:(NSString *)tileName AtPos:(CGPoint)tilePos onTileMap:(CCTMXTiledMap *)tileMap
{
  CCTMXLayer *floorLayer=[tileMap layerNamed:@"FloorLayer"];
  NSAssert(floorLayer !=nil, @"Ground layer not found!");

  CCSprite *baseTile = [floorLayer tileAt:tilePos];

    CCSprite *addedTile = [[CCSprite alloc] initWithFile:tileName];
    addedTile.anchorPoint = CGPointMake(0, 0);
    addedTile.position = baseTile.position;
    addedTile.vertexZ = baseTile.vertexZ;
    [tileMap addChild:addedTile z:baseTile.zOrder tag:tile.tag];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top