How To Build a Tower Defense Game for the iPhone – Part 2 – Placing Towers.
Welcome to part 2 of the tower defense series for the iphone using cocos2d – Today we’re going to be adding code to place towers. Whoa, slow down there you say – “place towers? That’s crazy talk”. I assure you it isn’t and once you are able to place towers in the next section we will combine our lessons together to have a single level of play! If you haven’t read “Cocos2d Game Tutorial – How To Build a Tower Defense Game for the iPhone – Part 1 – Creep Waves!“, you may want to review it before you continue on.
Ok, so one thing you may have learned from the last article was that making games isn’t like real life – it just need to act like real life. We didn’t need a complicated A*, DFS/BFS or Best-first searches to move our enemies around. In fact we didn’t need a tile based map at all. That doesn’t mean we still won’t need one in the future, but for now we follow the K.I.S.S system (keep it simple, stupid).
So lets take a look at what we are trying to recreate. Try to place the tower and see how it works – Please forgive me for my poor “gun tower” skills!
Source code after the break!
You can download the latest code for Cocos2d iPhone Game Tower Defense Tutorial Part 2. Once again, we’re using the Cocos2d framework which you can download here!
The way tower placement works is the player clicks and drags a tower from the “Game HUD” on a location on the map that is placeable. The tower also has its own respective range which is also visible after the user selects the tower from he game HUD. Now to the exciting part – If the user drop the tower on a sandy area we place one “machine gun tower” there. If the drop the tower anywhere else, we assume that the user didn’t want to click that tower in the first place and remove the tower placing option.
You can see that anywhere there is just sand, a tower can be places on that particular tile. The way we do that is through two functions – “tileCoordForPosition” and “addTower”. The function “tileCoordForPosition” is a quick way to determine the positon of the current tile we are over and we use that in the “addTower” function which actually places the tower on the map (assuming it is buildable there).
- (CGPoint) tileCoordForPosition:(CGPoint) position
{
int x = position.x / self.tileMap.tileSize.width;
int y = ((self.tileMap.mapSize.height * self.tileMap.tileSize.height) - position.y) / self.tileMap.tileSize.height;
return ccp(x,y);
}
-(void)addTower: (CGPoint)pos {
DataModel *m = [DataModel getModel];
Tower *target = nil;
CGPoint towerLoc = [self tileCoordForPosition: pos];
int tileGid = [self.background tileGIDAt:towerLoc];
NSDictionary *props = [self.tileMap propertiesForGID:tileGid];
NSString *type = [props valueForKey:@"buildable"];
NSLog(@"Buildable: %@", type);
if([type isEqualToString: @"1"]) {
target = [MachineGunTower tower];
target.position = ccp((towerLoc.x * 32) + 16, self.tileMap.contentSize.height - (towerLoc.y * 32) - 16);
[self addChild:target z:1];
target.tag = 1;
[m._towers addObject:target];
} else {
NSLog(@"Tile Not Buildable");
}
}
Page 1 of 5 | Next page

