Game Development Reference
In-Depth Information
CGSize screenSize = [CCDirector sharedDirector].winSize;
// Create the player and add it
player = [Player player];
player.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);
// offset player's texture to best match the tile center position
player.anchorPoint = CGPointMake(0.3f, 0.1f);
[self addChild:player];
return self;
You will also have to update the TileMapLayer interface to add the player in-
stance variable and forward-declare the Player class:
@class Player;
@interface TileMapLayer : CCLayer
CGPoint playableAreaMin, playableAreaMax;
Player* player;
The player's position is set to the center of the screen on purpose. Because you already
have a method that allows you to center a specific tile on the screen, centering the play-
er sprite on the screen as well makes it behave as if it were moving across the tilemap,
when in fact it always remains at the same position. You don't have to move the player
sprite at all!
The player's anchorPoint is offset a little from its default of (0.5f, 0.5f) to
(0.3f, 0.1f) to approximately center the sprite's feet on the center position of the
tile. Otherwise, it might look wrong because all other game objects like trees and cacti
have their roots, literally speaking, at the center of the tile. So, it's only natural to try to
place the player's feet at that position as well.
Search Nedrilad ::

Custom Search