Using a camera to follow the player

Hi. I’m trying a little demo in which I have a game with a ‘zelda perspective’. In it, I’m moving a player through the map, and now I want that when it is next to the limit of the screen, the “camera” moves to follow it. To do this, I’m trying to move the position of the map node, so the player will never be out of the screen.

Apparently, I’m having some trouble doing this. Looks like the movement I’m making to the map is slower than the player movement, which I do not understand. I’m taking the difference between the limits of the screen and the player to set the new position of the map. I think I’m having some trouble with the scale of the map node…

This is what I have:

mapNode (which has inside a TileMap and a Player).

This is the code I’m using:

CCPoint playerPos = _player->getPosition();
CCPoint mapPos = _mapNode->getPosition();
CCSize mapSize = _mapNode->getContentSize();

CCPoint newMapPos = mapPos;

float limitLeft = mapPos.x + mapSize.width*(1 - kMarginHorizontalPercent);
float limitRight = mapPos.x + mapSize.width*kMarginHorizontalPercent;

if(limitLeft > playerPos.x)
	newMapPos.x = (mapPos.x - (limitLeft - playerPos.x))*-1;
else if(limitRight < playerPos.x)
	newMapPos.x = (mapPos.x + (playerPos.x - limitRight))*-1;

float limitBottom = mapPos.y + mapSize.height*(1 - kMarginVerticalPercent);
float limitUpper = mapPos.y + mapSize.height*kMarginVerticalPercent;

if(limitBottom > playerPos.y)
	newMapPos.y = (mapPos.y - (limitBottom - playerPos.y))*-1;
else if(limitUpper < playerPos.y)
	newMapPos.y = (mapPos.y + (playerPos.y - limitUpper))*-1;

_mapNode->setPosition(newMapPos);

The mapNode has a scale applied to only show the amount of tiles I want. The size of the mapNode is the amount of tiles in screen that there are.

The problem is that the player is running faster than the map :scream_cat:. Also, I had to put *-1 because the movement was reversed, don’t know why…

Any help please? I feel that if I understand this, finally I might be starting to understand how the CCNodes work.

Thanks

Take a look at this tutorial:

And search for the section called “Tiled Object Layers and Setting Tile Map Position”

I think that should be able to help you out

I know that tutorial. In fact, I did some of the things it says.

The case is that I want to know why my solution is not working to learn about it. I don’t just want to take an already done solution.

you can arrange everything into a layer, call that layer your camera

if you move the player to the right by 100 pixels
you simply move that layer to the left by 100 pixels
your player will appear to be centered on the screen, just like a camera

We will add Camera feature in 3.3, stay tuned

Thanks for answering Wuhao.

What you are saying is what I’m trying to do. I have a node with the tilemap and the player inside. When the player moves, I move the node to the oposite direction, supposedly, the same amount of distance. The problem is the node is moving slower than the player, and I don’t know why.

My code looks a bit more complicated because I just want to move the node if the player is next to the limits of the screens, so I’m calculating if it has crossed a limit and if so, calculate how much it has crossed it and move the camera this amount.

the only explaination is that one of the layer or node you have have a different scale

@Frionel - Fair enough.

To expand on what @Wuhao said, the reason you’re having to multiply by -1 to get the right direction is that the camera and the map move oppositely relative to one another. To give you a concrete example, imagine reading text on a piece of paper through a magnifying glass. In this example, the paper is your map and the magnifying glass is the camera, or view port. If you’re reading from right to left, you can either 1) move the magnifying glass to the right or 2) hold the magnifying glass still and pull the paper to the left. The relative motions are opposite.

For your code, I have a few questions.

  1. Is the player added as a child to the CCTMXTiledMap or to the same CCLayer that the tiledMap is added?
  2. Is the tileMap using the default anchorPoint (0, 0), or are you setting it to something else?
  3. Is this code being called in a scheduled function?
  4. When/where does the player’s position get updated?

May be you can make the layer to run follow action with player. For more information, you can go to find out.