The TileMapAtlas object was introduced in cocos2d v0.5.0. Although it helped in creating big scenarios, a lot of functionality was missing, like the possibility to read and modify the atlas in runtime, isometric tiles, hex tiles, animations, etc.
From cocos2d v0.7 onwards, you can read/write the TileMapAtlas (the rest of the features will be included in v0.8)
So, how I do a read / write my TileMapAtlas in v0.7 ?
Let’s start by recalling how to create a TileMapAtlas. The API is same you were using since v0.5:
TileMapAtlas *tilemap = [TileMapAtlas tileMapAtlasWithTileFile:@"tiles.png" mapFile:@"levelmap.tga" tileWidth:16 tileHeight:16]; [self add:tilemap];
Since v0.7 you can read/write the TileMapAtlas but if you don’t want to, you might want to release the map from memory:
// Release the map from memory ONLY if won't read or write the tile map [tilemap releaseMap];
But if you are going to read or write the map, DON’T release the memory. Otherwise, you won’t be able to read or modify the atlas.
To read the the value of a certain tile, or to iterate over all the tiles you can do the following:
// For example you can iterate over all the tiles // using this code, but try to avoid the iteration // over all your tiles in every frame. It's very expensive for(int x=0; x < tilemap.tgaInfo->width; x++) { for(int y=0; y < tilemap.tgaInfo->height; y++) { ccRGBB c =[tilemap tileAt:ccg(x,y)]; if( c.r != 0 ) { // only the channel 'r' (red) is used // if r == 0 it means that it is transparent, it won't be rendered } } }
And to modify a certain tile of the TileMapAtlas object you can do as follows:
int x = 20; int y = 30; // read value ccRGBB c =[tilemap tileAt:ccg(x,y)]; // modify it c.r = 21; // and store it on the tile map atlas [tilemap setTile:c at:ccg(x,y)];
Limitations: you can’t modify a position whose tile value is 0. If you know that you are going to modify a certain position of the atlas, don’t set the initial value with 0.
For more information see the AtlasDemo example: TestAtlas.m


what is .tga, how i can make this. can you briefly explain all process from start to end
Any updates on Hex support in TileMapAtlas? I see in this post that it was expected to be ready by 0.8, but I haven’t read anything concerning it.
Thanks,
Sunsu
@sunsu: Hex support is not scheduled for v0.8.0
Tiless editor is scheduled for v0.8.1
v0.8.2 or v0.9 might include hex support.
i think there’s a problem with this syntax: [self add:tilemap]; the method ‘add’ is not supported or what? because my compiler said theres no method ‘add’
– cocos2d 0.8-rc
got it. add is an old version of cocos2d. addChild is the new one. sorry
where did the ccRGBB came from? COCOS2d? Cannot use ccRGBB
Hi, I’m doing a game for the iphone and i don’t know load a map -tga. Is only write:
TileMapAtlas *tilemap = [TileMapAtlas tileMapAtlasWithTileFile:@"tiles.png" mapFile:@"levelmap.tga" tileWidth:16 tileHeight:16];
[self addChild:tilemap];
?
This is correct if the tiles.png and levelmap.tga aree in the resources, are not?
Thanks for all !!
you must put all image and file you wanna load in the resources folder. The code for adding map is correct. I suggest you to use TMX map because it is very faster and recommended by cocos2d developer