/ Published in: Objective C
Use this variation of **setPlayerPosition** in your HelloWorldScene.m file to negate the need to use **setViewpointCenter** to manually calculate the viewport to track your player. This uses the **CCFollow** function as demonstrated in ActionsTest, one of the test apps included with cocos2D. Also to note is that the player movement is controlled by **CCMoveTo** which gives the player the same smooth movement around the map that the enemies have.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-(void)setPlayerPosition:(CGPoint)position { CGPoint tileCoord = [self tileCoordForPosition:position]; int tileGid = [_meta tileGIDAt:tileCoord]; if (tileGid) { if (properties) { if (collision && [collision compare:@"True"] == NSOrderedSame) { return; } if (collectable && [collectable compare:@"True"] == NSOrderedSame) { [_meta removeTileAt:tileCoord]; [_foreground removeTileAt:tileCoord]; self.numCollected++; [_hud numCollectedChanged:_numCollected]; } } } ccTime moveDuration = 0.3; id playerMove = [CCMoveTo actionWithDuration:moveDuration position:position]; id cameraMove = [CCFollow actionWithTarget:_player worldBoundary:CGRectMake(0, 0, (_tileMap.mapSize.width * _tileMap.tileSize.width), (_tileMap.mapSize.height * _tileMap.mapSize.height))]; [_player runAction:playerMove]; [self runAction:cameraMove]; }