cocos2d-iphonetileisometrickobold2d

How to add a tile dynamically in isometric map in Cocos2d-iphone


I have an isometric map made using Tiled Map editor. Current tile has only a base layer (i.e. the background).

I want to add a tile OVER this layer dynamically, say at tile position (1,1), such that the tile effectively becomes a part of the map.

So when I change the position of the map (e.g. while scrolling) I want the newly added tile to behave like the part of the map and move in its tile position with the map.

I tried CCTMXLayer classes -(void) addChild: (CCNode*)node z:(NSInteger)z tag:(NSInteger)tag; but it throws an error saying this. I dont quite understand the 2nd line.

addchild:z:tag: is not supported on CCTMXLayer. Instead of setTileGID:at:/tileAt:

Also, I tried adding an Object Layer in Tiled and accessed an objects properties -

CCTMXObjectGroup* objectLayer=[tileMap objectGroupNamed:@"ObjectLayer"];
NSDictionary *properties = [objectLayer objectNamed:@"theObject"];
int x = [properties[@"x"] intValue];
int y = [properties[@"y"] intValue];

Here I'm facing difficulty in converting from the x,y value to screen coordinates. Or from x,y to correct tilePos. Need urgent help here.

Side note - I read the Kobold Kit will have ability to "Create and modify every tilemap aspect at runtime". Will this help here?


Solution

  • If you want to add a tile to say tile (x,y) ( (x,y) in Tiled Map editor coordinates ) then use the following code -

    myTileMap is the reference to the CCTMXTiledMap object.

    CCTMXLayer *layer=[myTileMap layerNamed:@"yourlayer"];
    NSAssert(floorLayer !=nil, @"Ground layer not found!");    
    CGPoint tileAddPosition = [layer positionAt: CGPointMake(x,y)];
    
    //Create your CCNode or CCSprite or whatever...say object name is **tileToBeAdded**
    
    tileToBeAdded.anchorPoint = CGPointZero;
    tileToBeAdded.position = tileAddPosition;
    [myTileMap addChild:addedTile z:1];