Book Image

Learning Cocos2d-JS Game Development

By : Emanuele Feronato
Book Image

Learning Cocos2d-JS Game Development

By: Emanuele Feronato

Overview of this book

Table of Contents (18 chapters)
Learning Cocos2d-JS Game Development
Credits
Foreword
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Creating Your Own Blockbuster Game – A Complete Match 3 Game
Index

Controlling the spaceship


Players will be able to give thrust to the spaceship by holding the mouse or finger pressed on the screen.

As you should be able to detect when the player touches the screen, I am going to show you a mouse-only way to control the spaceship to make you learn something new. You are free to replace this way to control the ship with the one you prefer.

You are going to manage ship control in just a few lines, first by adding a new global variable:

var background;
var gameLayer;
var scrollSpeed = 1;
var ship;
var gameGravity = -0.05;
var gameThrust = 0.1;

The gameThrust variable represents engine power, the force that will make the ship fly through the air.

You are controlling the game with the mouse, so that's how you change the game declaration:

var game = cc.Layer.extend({
  init:function () {
    this._super();
    cc.eventManager.addListener({
      event: cc.EventListener.MOUSE,
      onMouseDown: function(event){
        ship.engineOn = true;
      },
      onMouseUp...