11: Working with Tilemaps in Objective-C

Generate DataMatrix in Objective-C 11: Working with Tilemaps

CHAPTER 11: Working with Tilemaps
Drawing DataMatrix In Objective-C
Using Barcode creation for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.
www.OnBarcode.com
QR Code 2d Barcode Creation In Objective-C
Using Barcode creator for iPhone Control to generate, create QR image in iPhone applications.
www.OnBarcode.com
the Layers view. Or, click the layer s check box to hide or unhide everything drawn on this particular layer. Just be sure to enable all layer check boxes before saving the TMX tilemap. Cocos2d does not load layers that are unchecked in Tiled. When you re done with this, you should have a tilemap similar to the one in Figure 10 9. Save it in the same TileMap01 Resources folder where the tileset images are.
Generating Barcode In Objective-C
Using Barcode generator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Drawing Code 3/9 In Objective-C
Using Barcode maker for iPhone Control to generate, create USS Code 39 image in iPhone applications.
www.OnBarcode.com
Figure 10 9. A completed tilemap with three tile layers and a object layer
GS1 - 13 Maker In Objective-C
Using Barcode creator for iPhone Control to generate, create EAN13 image in iPhone applications.
www.OnBarcode.com
GTIN - 128 Creation In Objective-C
Using Barcode generation for iPhone Control to generate, create EAN / UCC - 14 image in iPhone applications.
www.OnBarcode.com
Using Orthogonal Tilemaps with Cocos2d
Code 128 Code Set C Drawer In Objective-C
Using Barcode encoder for iPhone Control to generate, create Code 128A image in iPhone applications.
www.OnBarcode.com
Printing EAN8 In Objective-C
Using Barcode generator for iPhone Control to generate, create UPC - 8 image in iPhone applications.
www.OnBarcode.com
To use TMX tilemaps with cocos2d, you first have to add the TMX file and the accompanying tileset image files as resources to your Xcode project. In the TileMap01 project I added orthogonal.tmx along with the tilesets dg_grounds32.png and gameevents.png. Loading and displaying the tilemap is very straightforward; the following code is from the init method of the TileMapLayer class:
Encoding Data Matrix 2d Barcode In None
Using Barcode printer for Software Control to generate, create Data Matrix 2d barcode image in Software applications.
www.OnBarcode.com
ECC200 Maker In Java
Using Barcode creation for Eclipse BIRT Control to generate, create Data Matrix image in BIRT applications.
www.OnBarcode.com
CHAPTER 10: Working with Tilemaps
Encode Barcode In Java
Using Barcode creation for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Encode PDF 417 In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create PDF-417 2d barcode image in .NET applications.
www.OnBarcode.com
CCTMXTiledMap* tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"orthogonal.tmx"]; [self addChild:tileMap z:-1 tag:TileMapNode]; CCTMXLayer* eventLayer = [tileMap layerNamed:@"GameEventLayer"]; eventLayer.visible = NO;
Drawing Data Matrix 2d Barcode In Objective-C
Using Barcode printer for iPad Control to generate, create Data Matrix ECC200 image in iPad applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Scanner In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
The CCTMXTiledMap class is initialized with the name of the TMX file and then added as a child with a tag so that it can be retrieved later. A member variable would of course work just as well. The next step is to retrieve the CCTMXLayer used for game events by using the layerNamed method and providing the name of the layer as it was named in Tiled. Because the game events layer will be used only as hints for code to determine properties of certain tiles, this layer should not be rendered at all. Note that if you uncheck the layer in Tiled, it won t be displayed, but you will also not have access to its tiles and tile properties either. If you run the project now, you ll see a tilemap just like in Figure 10 10.
Make QR Code JIS X 0510 In None
Using Barcode generator for Font Control to generate, create QR image in Font applications.
www.OnBarcode.com
Creating GTIN - 13 In Java
Using Barcode maker for BIRT Control to generate, create EAN-13 Supplement 5 image in BIRT applications.
www.OnBarcode.com
Figure 10 10. The orthogonal tilemap in the iPhone Simulator
USS-128 Creation In Java
Using Barcode creation for Java Control to generate, create GTIN - 128 image in Java applications.
www.OnBarcode.com
Painting 2D Barcode In Java
Using Barcode creation for Java Control to generate, create Matrix 2D Barcode image in Java applications.
www.OnBarcode.com
Right now you can t do anything with the tilemap, but I d like to change that. Moving on to the TileMap02 project, I d like to be able to find the isWater tiles. I ve added the ccTouchesBegan method, as shown in Listing 10 1, in order to determine the tile that the player is touching.
Creating Code 39 Extended In .NET Framework
Using Barcode drawer for Reporting Service Control to generate, create Code 39 Full ASCII image in Reporting Service applications.
www.OnBarcode.com
Encode Data Matrix In .NET Framework
Using Barcode drawer for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
Listing 10 1. Determining a Tile s Properties -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CCNode* node = [self getChildByTag:TileMapNode]; NSAssert([node isKindOfClass:[CCTMXTiledMap class]], @"not a CCTMXTiledMap"); CCTMXTiledMap* tileMap = (CCTMXTiledMap*)node; // Get the position in tile coordinates from the touch location CGPoint touchLocation = [self locationFromTouches:touches]; CGPoint tilePos = [self tilePosFromLocation:touchLocation tileMap:tileMap]; // Check if the touch was on water (e.g., tiles with isWater property) bool isTouchOnWater = NO; CCTMXLayer* eventLayer = [tileMap layerNamed:@"GameEventLayer"]; int tileGID = [eventLayer tileGIDAt:tilePos];
CHAPTER 11: Working with Tilemaps
if (tileGID != 0) { NSDictionary* properties = [tileMap propertiesForGID:tileGID]; if (properties) { NSString* isWaterProperty = [properties valueForKey:@"isWater"]; isTouchOnWater = ([isWaterProperty boolValue] == YES); } } // Decide what to do depending on where the touch was if (isTouchOnWater) { [[SimpleAudioEngine sharedEngine] playEffect:@"alien-sfx.caf"]; } else { // Get the winter layer and toggle its visibility CCTMXLayer* winterLayer = [tileMap layerNamed:@"WinterLayer"]; winterLayer.visible = !winterLayer.visible; } }
The CCTMXTiledMap is retrieved as usual. The location of the touch is first converted into screen coordinates and then used to retrieve the tilePos containing the indices into the tilemap at that specific screen location. I ll get to the tilePosFromLocation method in a minute. For now just know that it returns the index of the touched tile. At this point I d like to introduce the concept of global identifiers (GIDs) for tiles, which are unique integer numbers assigned to each tile used in a tilemap. The tiles in a map are consecutively numbered, starting with 1. A GID of 0 represents an empty tile. With the tileGIDAt method of the CCTMXLayer, you can determine the GID number of the tile at the given tile coordinates. Next, the CCTMXLayer named GameEventLayer is obtained from the tilemap. This is the layer where I defined the isWater tile and drew it over the river tiles. The tileGIDAt method returns the unique identifier for this tile. If the identifier happens to be 0, it means there is no tile at this position on this layer in that case, it s already clear that the touched tile can t be an isWater tile. The CCTMXTiledMap has a propertiesForGID method, which returns an NSDictionary if there are properties available for the tile with the given identifier, or GID. This NSDictionary contains the properties edited in Tiled (see Figure 10 8). The dictionary stores any key/value pairs as NSString objects. If you want to see what s in a particular NSDictionary for debugging purposes, you can use a CCLOG statement like this:
CCLOG(@"NSDictionary 'properties' contains:\n%@", properties);
This will print out a line similar to the following in the debugger console window:
2010 08-30 19:50:52.344 Tilemap[978:207] NSDictionary 'properties' contains: { isWater = 1; }
Copyright © OnBarcode.com . All rights reserved.