Posted By

cell-gfx on 08/30/10


Tagged

cocos2d ios


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

alfredbaudisch
umang_nine


Setting cocos2D camera position using CCFollow


 / 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.

  1. -(void)setPlayerPosition:(CGPoint)position {
  2. CGPoint tileCoord = [self tileCoordForPosition:position];
  3. int tileGid = [_meta tileGIDAt:tileCoord];
  4. if (tileGid) {
  5. NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
  6. if (properties) {
  7. NSString *collision = [properties valueForKey:@"Collidable"];
  8. if (collision && [collision compare:@"True"] == NSOrderedSame) {
  9. return;
  10. }
  11. NSString *collectable = [properties valueForKey:@"Collectable"];
  12. if (collectable && [collectable compare:@"True"] == NSOrderedSame) {
  13. [_meta removeTileAt:tileCoord];
  14. [_foreground removeTileAt:tileCoord];
  15. self.numCollected++;
  16. [_hud numCollectedChanged:_numCollected];
  17. }
  18. }
  19. }
  20.  
  21. ccTime moveDuration = 0.3;
  22. id playerMove = [CCMoveTo actionWithDuration:moveDuration position:position];
  23. id cameraMove = [CCFollow actionWithTarget:_player worldBoundary:CGRectMake(0, 0, (_tileMap.mapSize.width * _tileMap.tileSize.width), (_tileMap.mapSize.height * _tileMap.mapSize.height))];
  24. [_player runAction:playerMove];
  25.  
  26. [self runAction:cameraMove];
  27.  
  28. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on January 12, 2011

[collision compare:@"True"] == NSOrderedSame should be written as [collision isEqualToString:@"True"]

Posted By: Laoba on December 15, 2012

Wow, I needed this and it's for that exact project that you gave the code. Thanks a ton!

Posted By: Laoba on December 15, 2012

By the way, for iPhone 5 I have to change CCFIXARTIFACTSBYSTRECHING_TEXEL from 0 to 1 in the ccConfig.h file due to the border of some tiles appearing while moving.

You need to login to post a comment.