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.