Help with Make Camera follow player

Hello!

I’m trying to make a zelda-like game(2d like Link to the Past). So far got the level tiles to work, and spawn point. But I cannot make the screen or camera to be focused on the character, and when it moves to also keep the character in the center of the screen.

Tried using Camera Node, but so far no luck.

Thanks!

I experimented with the Follow action the other day and it appears to do this. Have you tried this? Give the character as a parameter and have the layer run the action.

I was reading that the follow action is not recommended for games with defined levels(such as ones made with tiled). I’m not completely sure about that tho.

Because I like to do things myself, whenever I move the character, I would call a function to move the world node so that it places the character at the center of the screen

void MyClass::moveWorld() {
    auto ws = characterNode->getParent()->convertToWorldSpace(characterNode->getPosition()); // 1
    auto ns = worldNode->getParent()->convertToNodeSpace(ws); // 2
    worldNode->setPosition(worldNode->getPosition() - ns + Director::getInstance()->getWinSize() / 2); // 3
}
  1. Get the current position on screen of the character.
  2. Calculate how far the character is from the origin of the world node.
  3. This involves:
    a. Get the position of the world node.
    b. ns is how far the character is from the origin of the node, so if you add -ns to the world node’s position then it will move the character to the bottom left of the screen.
    c. Adding half the size of the screen to the position will move the node to the center of the screen.

I just tested that code and it seems to work for me. And if the parent of the world node is the scene node, then you can reduce the code to

auto ws = characterNode->getParent()->convertToWorldSpace(characterNode->getPosition());
worldNode->setPosition(worldNode->getPosition() - ws + Director::getInstance()->getWinSize() / 2);

But if you find a better way to do it or anyone else has suggestions, I’d love to hear them too :smiley:

I use CCFollow and it works great for tracking a sprite across the screen.

I created a sample game that follows a 2D side scrolling race car on a race track using the camera.

In the update method I just move the camera to the location where the vehicle is, at a certain distance away from the vehicle in the Z direction, and have the camera look at the vehicle/character’s position.

The sample source code actually uses three cameras. The default one for UI, a second camera for the race car and race track ground, and a third camera for the background.

You can download the source for free, but I’m currently only giving it out to people that join my email list.
https://heyalda.leadpages.co/car-racing-game-source-code-heyalda/
In full disclosure, if you join my email list, I will send you free cocos2d-x cocos2d-x development resources, tips, and product offers of things that I sell relating to making games with Cocos2d-x.

Here is a video of what the source code does:

Thanks to everybody, specially Grimfate. Got it to work with Follow Action.

Thanks a lot!

Hi heyalda, I want your camera move source code(“2D side scrolling race car on a race track using the camera”), my email is 358445267@qq.com, free or not free are all ok. thank u:) if not free, give me you card number.

Whats worldNode ?

Broken Link - Broken Video ?

The world node is the level; basically everything but the U.I. It’s everything that will move around your character when it moves.

How do i declare it, I get error with that part of code. What do I do to handle it, is it Scene* ?

It won’t be Scene*, because that will move the U.I. as well. It would be something like this:

Scene

  • WorldNode
    – Character
  • UI

So I should have a World Node Layer… So Far I have added everything like this

this->addChild(everything); ?

What is this in your example? Is it the Scene*?

Dunno, what would it be in the hello world example ?

Where do you set what it is ?

If you are using HelloWorld then you can probably just replace worldNode with this.

Well I would have maybe started with them classes and came long way since.

worldNode doesn’t come up as initialized, so means I do not have it

worldNode is just an example name for the node that holds your world. There is no node included with HelloWorld or cocos called worldNode.

So it is a layer ? And I add all my stuff to a layer instead of a “this”