Scanning through a tilemap and checking properties for each tile
How does one iterate through a tilemap and check each tile?
Is there a correct way to do this, is there a built in function to in cocos2d to check a tile?
Or could it be done e.g. take the tile size set when creating the tile, make a nested for loop and take (x,y) for the middle of the first tile and just iterate by adding tilesize to the x on the inner loop and tilesize to the y on 开发者_C百科the outer loop?
I am wondering if there is a built in, more performance aware approach.
Thanks
I think you might be able to do it using a for loop and CGPoints. I'm going to for examples sake get color and store it in an array I guess
CGPoint myPt;
NSMutableArray *tilesofGray;
for (int x = 0; x < tilemapLength)
{
for (int y = 0; y < tilemapHeight)
{
myPt.x = x;
myPt.y = y;
if([[[tilemap layerNamed:@"background"] tileAt:myPt] getColor] == Grey)
{
[tilesofGray addObject:[[tilemap layerNamed:@"background] tileAt:myPt]];
}
}
}
Is this for a game, for like collision detection or, simply for rendering based on tile type?
Your question here is really ambiguous. Please be specific in what you want. The 3rd sentence in particular would make more sense if you explain what you are needing.
But i'll try to answer based on the title alone....
- How big is the tileset? if it's not very big, brute-force may be perfectly fine.
- If performance is a concern/issue, or if the tileset is large and not all tiles are ever drawn within the screen at any given time, you need to do scene management of some sort.
scene management: i think there is a technical term/phrase for this, but basically based on some x,y pt on the tileset (i.e. matrix), you can determine (by a function) which tiles you will need to iterate thru. it should be fun to figure it out as it's presumably a 2d array.
精彩评论