Author Archive

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.

Categories: General Tags: , ,

Turning CGPoints into NSStrings

July 2, 2011 Comments off

I’m the master of the good old printf debugger.  I guess I’m dating myself though, because WTF uses printf when you’ve got NSLog?

Anyway, I’m constantly dropping NSLogs when I’m messing with CGPoints.  And I first stupidly did one of these:

CGPoint killBieber = ccp(priceOfTeaInChina, imRickJamesBitch);
NSLog(@"This is your fucking point: %f",killBieber);

Of course that doesn’t work; I’m an idiot/need less blood in my caffeine system.  So I did this:

NSLog(@"This is really your stupid point: %@",killBieber);

The %@ is awesome, because it dumps the object in your string.  It works great if you have an NSString or an NSDictionary.  Here, it blows up.

My first impulse was to do something like this:

NSLog(@"This is really your stupid point: %f,%f",killBieber.x,killBieber.y);

And that works, but it’s just as ugly and inconvenient as the performer I’m referring to in that variable.  Fortunately, there’s a better way.  Check this shit out:

NSLog(@"This is really your stupid point: %@",NSStringFromCGPoint(killBieber));

It’s a touch longer, but autocomplete is your friend here.  The sweet part of this is that there’s also a CGPointFromString, which saved my ass when I had to shove a bunch of objects into an NSMutableDictionary and use points as the key.

There are a bunch more functions if you need to do the same with sizes, rectangles, and whatever other CGCrap you are dealing with.

See also:

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

Hello, world

July 2, 2011 Comments off

This blog won’t make sense for a bit.

I’ve been learning how to write iOS apps lately, and need a place to drop bits of stuff I’ve learned so I don’t need to re-research them a week later when I forget them.  (I drink out of aluminum cans, and I’m over 40, so this happens a lot.)

Eventually, this will turn into a little knowledge base that may be useful to others.  Right now, bear with me.

Categories: General