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.
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
Get the current position on screen of the character.
Calculate how far the character is from the origin of the world node.
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
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.
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.