Cocos2D-X Platformer tutorial Part 5 – Camera

You know, to tell you the truth when I’m writing this I’m actually rediscovering cocos2d-x myself :D, how to do some things easier, but then again I only did board games on cocos2d-x … anyways back to tutorial.

Camera in cocos2d-x follows a certain target, normally this would be the player, but you need to define some boundaries to center the camera. This is actually a problem, because if I define boundaries I need to set the size of the playfield. I don’t like boundaries and the tmx map can have more than one map in it so screw that.

I defined a boundless camera which is going to follow an invisible target, this target’s y value will be always centered and x value will change according to player’s x value.

New stuff

  • cameraTarget – invisible target that camera follows
  • camera – camera object, don’t ask me why the class is called Follow
  • oh yeah and camera is run by the layer itself

2 responses to “Cocos2D-X Platformer tutorial Part 5 – Camera”

  1. Joshua Beitler says:

    Great tutorial! How would you recommend placing HUD elements like a CCLabel outside of this camera to prevent them from being moved when the player moves?

    • metin says:

      Hi, thanks
      Just add a new Layer(which is your HUD) to the scene object. The camera should not move the secondary layer. I suggest create a new layer class with your hud elements and methods and define it as a field in GameScene class. Then create an instance in createScene method and add it to the scene.

Leave a Reply

Your email address will not be published. Required fields are marked *