Archive

Posts Tagged ‘tilemaps’

Adding tiles to an empty Tilemap

July 2, 2011 Comments off

Okay, so I created a nice CCTMXTiledMap for a game.  It’s got two layers: a foreground and a background, each with their own tileset.  I created it in Tiled, and on the background layer, I drew all my happy little trees and water and mountains.  I left the foreground blank.  The plan was to later use setTileGIDat to change tiles in the foreground to little UFOs or aliens or zombies or whatever.

That didn’t work.  I kept getting errors at runtime . (I forget the error, and I’m not about to go back into my code and re-break it to cut and paste one in here.  It was something like “invalid texture”.)

So here’s the hack I used.  I added one tile to the foreground, down in the lower left corner.  (Add it anywhere, I did it at 0,99.  Maybe I’m a lower-class leftist, I don’t know.)  Then in your init, delete that tile with a removeTileAt, and you’ve got a blank layer that will let you set tiles.

Is there some better hack to do this?  You tell me.  I don’t know if there’s a hit on performance or memory during init for doing this, but it works for now, so on to the next fire.

Advertisements
Categories: General Tags: , ,

CCTMXLayer atPosition

July 2, 2011 Comments off

If you’re using a tilemap, and you have the position of a tile (like, 3,4) and you want to turn it into a screen position (348,567 or whatever), you can do this:

CGPoint tilePosition = [_myMapLayer positionAt:tileLoc];

This gets weird though if you decide to drop a sprite there.  This gives you the position of the upper left hand corner of that cell; sprite position (unless you mess with the anchor point) refers to the center of the sprite.

So if you have a 32×32 sprite and 32×32 cells, it’s going to end up 16 off in each direction.

This is easy enough to fix:

CGPoint tilePosition = [_foreground positionAt:tileLoc];
tilePosition = ccp(tilePosition.x+(_tileMap.tileSize.width/2),tilePosition.y+(_tileMap.tileSize.height/2));

Note that this puts your sprite’s center in the center of the tile.  If your sprite is the same size as the tile, great.  If your tile is 25×25 and the sprite is 50×50, it’s going to hang off each side of the cell like a fat man in a Southwest plane seat.

Is there a cleaner way to add two CGPoints?  You tell me.  I guess I could hack together my own little helper function?

Categories: General Tags: , ,