Posts Tagged ‘sprites’

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: , ,