.net barcode generator code project 10: Working with Tilemaps in Objective-C

Creating Data Matrix 2d barcode in Objective-C 10: Working with Tilemaps

CHAPTER 10: Working with Tilemaps
Encoding Data Matrix ECC200 In Objective-C
Using Barcode creator for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
Painting Barcode In Objective-C
Using Barcode encoder for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Figure 10 11. The coordinate system of an orthogonal tilemap
Making Code 128B In Objective-C
Using Barcode maker for iPhone Control to generate, create USS Code 128 image in iPhone applications.
www.OnBarcode.com
Data Matrix ECC200 Maker In Objective-C
Using Barcode encoder for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
So as not to get confused, this is the line of code that calculates the tile coordinate s x position:
Generate Barcode In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Barcode Drawer In Objective-C
Using Barcode generation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
pos.x = (int)(pos.x / tileMap.tileSize.width);
Encoding Code 3/9 In Objective-C
Using Barcode drawer for iPhone Control to generate, create ANSI/AIM Code 39 image in iPhone applications.
www.OnBarcode.com
UPC - E1 Encoder In Objective-C
Using Barcode generator for iPhone Control to generate, create UPC-E Supplement 5 image in iPhone applications.
www.OnBarcode.com
The tileMap.tileSize property is the size of the tiles in the tileset, which in this case is 32 32 (see also Figure 10 6). If the touch were at the 340 x position, the calculation would reveal the following:
DataMatrix Creator In Objective-C
Using Barcode encoder for iPad Control to generate, create ECC200 image in iPad applications.
www.OnBarcode.com
Data Matrix 2d Barcode Maker In None
Using Barcode printer for Font Control to generate, create DataMatrix image in Font applications.
www.OnBarcode.com
340 / 32 = 10.625
Data Matrix ECC200 Printer In None
Using Barcode printer for Microsoft Excel Control to generate, create Data Matrix 2d barcode image in Excel applications.
www.OnBarcode.com
Scanning Data Matrix In Visual C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
That can t be right, though. We re looking for a tile s x coordinate, which is never a fractional number! The reason is, of course, that the touch was somewhere inside the tile we re looking for (i.e., inside a 32 32 square area). The simple trick of casting the result to an int value will get rid of the fractional part and assign this to pos.x:
EAN13 Generator In .NET
Using Barcode encoder for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.
www.OnBarcode.com
ECC200 Generator In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
pos.x = (int)10.625 // pos.x == 10
Barcode Drawer In Java
Using Barcode maker for BIRT Control to generate, create Barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Data Matrix Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Casting to an int will remove the fractional part. You can safely get rid of the fractional part because it s simply not relevant actually it s harmful. If you didn t cast away the fractional part but used the noninteger coordinate, in this example 10.625, to try to retrieve the tile at a tile coordinate 10.625, you d receive a runtime error because there is only a tile at x coordinates 10 and 11, not at 10.625.
Barcode Generator In C#
Using Barcode encoder for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
PDF417 Maker In C#
Using Barcode generator for .NET framework Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
CHAPTER 11: Working with Tilemaps
Drawing 2D In Java
Using Barcode generator for Java Control to generate, create Matrix Barcode image in Java applications.
www.OnBarcode.com
Scan Barcode In VS .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
A slightly more complicated calculation is used to get the tile s y coordinate:
pos.y = (int)((tileMap.mapSize.height * tileMap.tileSize.height - pos.y) / tileMap.tileSize.height);
Note that the parentheses are important to make sure that the division is done last. In actual numbers, this calculation may be easier to understand. As shown in Figure 10 5, the tileMap.mapSize is 30 20 tiles, and as I mentioned earlier, tileMap.tileSize is 32 32 pixels. The calculation then looks like this:
pos.y = (int)((20 * 32 260) / 32)
Multiplying tileMap.mapSize.height with tileMap.tileSize.height returns the full height of the tilemap in pixels. This is necessary because the tilemap starts counting y coordinates from top to bottom, whereas screen y coordinates count from bottom to top. By calculating the bottommost y coordinate of the tilemap, and subtracting the current y position 260 from that, you get the correct y position of the touch into the tilemap, in pixels. And because it is a pixel coordinate, you need to divide by the tileSize.height and then cast down to an int value to get the tile s y coordinate. The CCLOG and NSAssert lines are helpful for seeing the results of the calculation in the debugger console, as well as ensuring that tile coordinates never take on illegal values. It s both a learning tool and an insurance policy.
An Exercise in Optimization and Readability
Since the tilemap s size never changes, you can optimize the calculation a little to get the y tile coordinate by adding a member variable to the class s @interface, which will be used to store the tilemap s height in pixels:
float tileMapHeightInPixels;
You can then make the calculation to get the tileMapHeightInPixels just once in the init method, right after the tilemap is loaded:
CCTMXTiledMap* tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"orthogonal.tmx"]; tileMapHeightInPixels = tileMap.mapSize.height * tileMap.tileSize.height;
Then you can rewrite the calculation, saving a multiplication every time you call the tilePosFromLocation method:
pos.y = (int)((tileMapHeightInPixels - pos.y) / tileMap.tileSize.height);
It may not win any awards for best optimization ever it s only a very tiny improvement in performance. But every bit counts, and it does make the calculation easier to read by taking away complexity and putting a readable variable name in its place.
Working with the Object Layer
The orthogonal.tmx tilemap I ve created as an example tilemap for this chapter also contains an object layer, fittingly named ObjectLayer. You can create object layers in Tiled by choosing Layer Add Object Layer. Then you can click inside the tilemap and
Copyright © OnBarcode.com . All rights reserved.