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

Preventing the spaceship from flying off the screen


The last thing you need to do is prevent the spaceship from flying off the screen. If you press the mouse for too long, or if you don't press the mouse at all, your ship will fly respectively off the top or the bottom of the screen.

You need to prevent the ship from flying off the screen by punishing it with death.

Just add these two lines to the ship's updateY function:

updateY:function() {
if(this.engineOn){
    this.ySpeed += gameThrust;
  }
if(this.invulnerability>0){
    this.invulnerability --;
    this.setOpacity(255-this.getOpacity());	
  }
this.setPosition(this.getPosition().x,this.getPosition().y+this.ySpeed);
  this.ySpeed += gameGravity;
if(this.getPosition().y<0 || this.getPosition().y>320){
    restartGame();
  }
}

No need to comment them, it's just an if statement that checks for the spaceship's vertical position.