Stacky Bird Built on Cocos2d-x
A replication of an old mobile game Stacky Bird made on the Cocos2d-x game engine in 1 day! Cocos2d-x is an open-source game engine that is very lightweight and fast. With the mind to replicate the core mechanics from an old game with better performance, I started this project.
Specs | Date |
---|---|
C++ Cocos2d-x | 05/2024 |
Details
Cocos2d-x is an open-source game engine that is very lightweight and fast due to the core engine being completely written in C++. With the mind to replicate the core mechanics from an old game with better performance, I started this project.
I have never used this game engine before, so I had to learn from scratch.
Game Systems:
As I was used to have a game object representation to handle their own callbacks, I created a simple GameObject class:
Notice that the On Collision callback in the Game Object class takes in two parameters. I had the collision callback implemented this way (below) so that it can work just like in Unreal (having access to the collision information and the reference to the other collider):
Since there is no native way for the Physics Body to get a reference to the Game Object it was attached to (Of course! The whole GameObject class was created by me!), I had the scene store a map with a pointer to a PhysicsBody as the key, and a pointer to a GameObject as the value:
This way, the input callbacks, collision callbacks, and the update() were able to get a referece to all the game objects in the scene:
Obstacles:
All obstacles were derived from the ObstacleBrick class, which was derived from the GameObject class. The ObstacleBrick class contains a sprite member because an obstacle must have a sprite. Its constructor not only initializes its sprite, but also its physics body.
Since the most complex process was handled in the ObstacleBrick base class, all other kinds of obstacle bricks could remain very simple since they derive from the ObstacleBrick class.
Garbage Collection:
I made a Garbage Collection Brick that derives from the SpikeBrick class. It get placed at somewhere outside the screen, and will brake any Eggs that touches it.
Bird:
The bird derives from the GameObject class. One thin worth mentioning is that, for performance, I avoided the use of the update() function to implement the "hold to keep jumping (lay eggs)" mechanics. Instead, I implemented it by using the Cocos2d-x's action manager:
So on touch begin, it will schedule an action that repeats forever with the interval of 0.2 s (defined by the macro SPAWN_EGG_INTERVAL_WHILE_HOLDING) to call the SpawnEgg(). Then, I set a tag to that scheduled action so that the action can be stopped by the tag when the user lifts their finger: