Game Development Reference
In-Depth Information
// get the pixel coordinates for a tile at these coordinates
CGPoint scrollPosition = [layer positionAt:tilePos];
// negate the position to account for scrolling
scrollPosition = ccpMult(scrollPosition, -1);
// add offset to screen center
scrollPosition = ccpAdd(scrollPosition, screenCenter);
// move the tilemap
CCAction* move = [CCMoveTo actionWithDuration:0.2f position:scrollPosition];
[tileMap stopAllActions];
[tileMap runAction:move];
First, the screen center position is determined as before. Then you want to use the con-
venience method of the layer, positionAt , which returns a screen position for a tile
coordinate. To do so, get the Ground layer and assert that it exists. It doesn't matter
which layer you use, as long as all layers use the same size tiles.
Before calling the positionAt method, I have to subtract 1 from the tile y coordin-
ate to fix a persistent offset problem. Seasoned programmers may be worried that using
a tile y coordinate of 0 and subtracting 1 from it could lead to an invalid index and thus
a disastrous crash. But in this case, the positionAt method doesn't use the tile co-
ordinates as indices, and it works with any tile coordinate, even negative ones.
The positionAt method returns the pixel position of the given tile coordinate with-
in the tilemap and stores it in the scrollPosition variable. This method isn't spe-
cific to isometric tilemaps; it works for all tilemap types: orthogonal, isometric, and
hexagonal. Internally, cocos2d checks which type of tilemap is currently being used
and then uses the appropriate calculation, because they differ in profound ways. If
you're interested in the specific implementation of each of these calculations, take a
look at the methods positionForOrthoAt , positionForIsoAt , and posi-
tionForHexAt in the cocos2d CCTMXLayer.m implementation file.
Because the tilemap may be scrolling, in which case it will have a negative position,
the scrollPosition is multiplied by −1, negating it. After that I just add the
screenCenter position to it, and I know where to scroll to. The move action is the
same as before and moves the tilemap so that the touched tile is centered onscreen.
Search Nedrilad ::

Custom Search