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).
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
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.
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.
Is the player added as a child to the CCTMXTiledMap or to the same CCLayer that the tiledMap is added?
Is the tileMap using the default anchorPoint (0, 0), or are you setting it to something else?
Is this code being called in a scheduled function?
When/where does the player’s position get updated?