Pinvaders


A super simple pinball game made with CocosCreator and just a few lines of code. This prototype contains no special effects, just the bare bone minimum. Perfect for new CocosCreator users.
This game uses mostly the physics component from CocosCreator. Which makes it pretty easy to create complex physics bodies and play with their properties. Only the camera component and the enemy prefab use scripts, and in this case just a few lines of code.

 

Download the project and examine the node structure and components.

 

Setup

We simply set the scene up by drag and drop items from the asset library to the scene. We create a script called gameController.js which we attach to the canvas and act as main script in this game.

 

Add physics

First we need to enable the physics engine with a script. We add this lines to the gameController.js file inside the onLoad() function.

Once the physics engine is enabled we can start to add physics bodies to sprites, add theme where necessary.

Add a sprite to the scene and add the physics component “box collider” to the sprite.

 

Add controls

To control the flippers on the left and on the right side we need to assign them to a script. We can do that by adding properties to our gameController.js script.

Once we have assigned the sprites to the script accordingly we can access and manipulate them via script now. We simply change the rotation of the sprite once we get a keyboard input-down or input-up event.

Assigne sprite to script

 

Add enemies

The last thing to add are the enemies. We create first a prefab of a simple enemy sprite with a box collider and a script called enemyScript.js attached to it. Just drag a node back into the file explorer to create a prefab.
The script enemyScript.js checks for any collision. If there is a collision detected the node will destroy itself.

Create prefabs

 

Add camera

A camera is essential in a physics driven world. We need to add a camera component to the main level node as well as a script to control this camera component. We need a target to follow, just add a new property to the script called target. Once we have assigned a target, we just follow this node within a min and max value.

Full camera script

 

What now

This is just a working prototype and far away from a complete game. There are still a lot to do, like effects, score, levels, graphics, etc.. But it’s a good starting point.
CocosCreator can now export this game to html5, iOS, Android and desktop.