cocos2d/cocos2d-iphone — pull request #302 by tompave (2013-04-02 05:20:27+00:00)
Title: implemented support for Tiled  tileset offset feature

The Tiled Map Editor (www.mapeditor.org) has implemented support for tileset offsets: https://github.com/bjorn/tiled/issues/16
(there is a screenshot in the above link that makes the feature very clear)

The Property is defined in this form (should be doubled for retina):

(...)

Cocos2d, however, doesn't pick up that property from the TMX file format.

Some workarounds are:

* Add the offset to GL touch coordinates when working with tiles (hit tests, etc)
* Add the offset to the `CCTMXTiledMap` node's position
* Add the offset to other sprites' positions if they need to be centered on the tiles
* etc...

I've added few lines (≈ 20, including comments) to the CCTMX related files to make Cocos2d read that new TMX property and adjust tile sprite positions accordingly.
The Commit: https://github.com/tompave/cocos2d-iphone/commit/5232cce1a492f0ca4816858cc16e127d3ecf5785

The `CCTMXXMLParser` now reads the the information and stores the offset (as pixels) in a `CGPoint` property of the `CCTMXTilesetInfo` class.
Upon setting the property, the custom setter calculates the relative anchorPoint (as range `0.0f - 1.0f`) and stores it in another CGPoint property of the `CCTMXTilesetInfo` class.
Finally, the `CCTMXLayer` instance owning the `CCTMXTilesetInfo` instance use the information to set the anchorPoint of individual tile sprites.

Comments:
* The process is automatic and, if the TMX file doesn't indicate any tileset offset, defaults to the standard `CGPointZero`.
* The "map grid" stays in place.
* All map functions keep working as usual, e.g. hit testing a tile with touch coordinates.
* All TileMap tests within the cocos2d repository still pass.
* I'm not sure if applying the offset to the tile sprites' `position` (rather than the `anchorPoint`) would be better. Suggestions would be welcome.

As an example, look at this picture:
![tile_comparison](https://f.cloud.github.com/assets/2141406/326862/b267388c-9b51-11e2-9658-458d41f043b0.png)
iPad simulator, non-retina.
The map is placed at the precise center of the display rect.
Map Properties:
* Grid tile dimesions: 100 x 50 pixels
* Tileset tile dimesions: 100 x 100 pixels (they have vertical decorative elements)
* __Tileset tile offset: x=0 y=15, the tile sprites are shifted 15px down__
* __The grid should be aligned with the top of the "grass" tiles. Water should be lower, stone should be higher__

The red line (aligned with the home button):
* indicates the half-height of the display
* overlaps the leftmost corner of the tilemap (tile coords: 0,9 for a 10x10 map)
* should orizontally bisect tiles of the central orizontal row

Without the optimization descibed above, the tile sprites of the `CCTMXTileMap` are rendered "normally" and __appear 15px above the actual tile grid__.
With the optimization, tile sprites are rendered according to the tile offset declared in the TMX file and the tile and the map grid overlap as expected.

Needless to say, the tileset offset should be based on the sprites' art.