Game Development Reference
In-Depth Information
screen coordinates increase from bottom up. In effect, I convert the tilePos y co-
ordinate as if it were counted from bottom up. In addition, I subtract 1 from the map's
height to account for the fact that tile coordinates are counted from 0. In other words, if
the map's height were 10, only the tile coordinates 0 to 9 would be valid.
The tilemap sizes (which are in pixel coordinates) are converted to points just as shown
earlier in Listing 10-3. This ensures that scrolling works on both standard-resolution
and Retina-resolution devices.
Next, create the scrollPosition CGPoint , which will become the position the
tilemap will be moved to. The first step is to multiply the tile coordinates with
pointWidth and pointHeight , respectively. You may be wondering why I negate
the tilePosInPixels coordinates. It's simply because if I want the tiles to move
from top right to bottom left, I have to move the tilemap down and to the left by de-
creasing the coordinates.
The next big block modifies the coordinates of the scrollPosition to center the
tile on the screen's center point. You also need to take into account the center of the tile
itself, which is why half the tileSize is deducted from the screenCenter offset.
Using the Objective-C language's MIN and MAX macros ensures that the
scrollPosition is kept within the bounds of the tilemap so as to not reveal any-
thing past the borders of the tilemap. MIN and MAX return the minimum and maximum
values of their two parameters and are a more compact and readable solution than con-
ditional assignments using if and else statements.
Finally, use a CCMoveTo action to scroll the tilemap node so that the touched tile is
centered on the screen. The result is a tilemap that scrolls to the tile that you tap. You
can use the same method to scroll to a tile of interest—for example, the player's posi-
Tip As for the player character itself, the next chapter has an implementation
of isometric tilemaps. You can apply the same principle to orthogonal tilemaps.
And this cocos2D forum thread will get you started with pathfinding on ortho-
gonal tilemaps, source code included:
um/topic/19463 .
Search Nedrilad ::

Custom Search