Make Your Own iPhone Game, Day 8


Skeletor Burnin’ the Rug

Tiled has a beautiful object layer where you can put characters, treasure chests, warp points to other levels and whatever else you can imagine. You just create an object layer then click on the object layer to create an object. Give it a distinct type so you can instantiate the object in your code.

For example, in my first level, I have a “player” object and a “skeleton” object. Here’s some of the code that instantiates the characters:

// explore object layer
NSMutableArray* ra = [objects objects];
for(i = 0; i < [ra count]; i++)
{
	// get this object
	CCTMXObject* o = [ra objectAtIndex:i];

	// the tmx map will instantiate objects of known classes
	// in trying this out it was too tricky and less control
	// so we only mess with objects of type CCTMXObject
	if([o class] == NSClassFromString(@"CCTMXObject"))
	{
		// loop over the object's properties
		NSMutableDictionary* props = [o properties];
		id type = [props valueForKey:@"type"];

		// setup the player
		if([type isEqualToString:@"player"])
		{
			Player* player = [[[Player alloc] init] autorelease];
			player.position = [self propsXYToPoint:props];
			[self addChild:player z:1 tag:kTagPlayer];
		}

		// setup a skeleton
		if([type isEqualToString:@"skeleton"])
		{
			Skeleton* skeleton = [[Skeleton alloc] init];
			skeleton.position = [self propsXYToPoint:props];
			[self addChild:skeleton];
		}
	}
}

Leave a Reply

Spam protection by WP Captcha-Free

Storyteller   .:.   Musician   .:.   Vagabond   .:.   Wizard