SUPERPOWERS TUTORIAL #4

SUPER ASTEROIDS and SUPER SPACEWAR, Chapter 10

Polishing the game

Background scrolling

We create a scrolling movement for the background in the Background script.

class BackgroundBehavior extends Sup.Behavior {
  // Initialize position and speed variables
  position: number;
  speed : number;

  awake() {
    // Get the position on Y axis of the actor
    this.position = this.actor.getLocalY();
    // Set the speed of background scrolling
    this.speed = 0.01;
  }

  update() {
    // Add the speed movement to the current position
    this.position += this.speed;
    // Set the current position to the actor
    this.actor.setLocalY(this.position);

    // If the sprite moved all its height (480/16 = 60 units)
    if (this.position > 60) {
      // Then return down to start again
      this.position = -60;
    }
  }
}
Sup.registerBehavior(BackgroundBehavior);

Sounds and Music

Note : The music is still in development, the tutorial will be updated when ready

We add all the sounds in differents place of our script :

  • When the player use the mouse on a button in the menu.
  • When the alien shoot a missile.
  • When the ship missile touch the alien ship.
  • When the ship shoot a missile.
  • When the player ship, an asteroid or the alien ship is destroyed.
[...]
  update(){
    // If the button 1 is hovered
        if(hitB1[0]){
        [...]
        // If the left mouse button is pressed
        if (Sup.Input.wasMouseButtonJustPressed(0)) {
            Sup.Audio.playSound("Sounds/shipShot");

    // If the button 2 is hovered
        if(hitB2[0]){
        [...]
        // If the left mouse button is pressed
        if (Sup.Input.wasMouseButtonJustPressed(0)) {
            Sup.Audio.playSound("Sounds/shipShot");
[...]
Alien script
[...]
  update() {
  [...]
    // Set death if alien don't have lifes anymore
    if(Alien.lifes === 0){
      Sup.Audio.playSound('Sounds/explosion');
      [...]

    // Keep shooting
    // If the timer is not finished, decrease value of one
    if (this.shootCooldown > 0) {
      this.shootCooldown--;
    }
    // If the timer is finished, shoot missile
    else {
      Sup.Audio.playSound('Sounds/alienShot');
      this.shoot();
    }
    [...]

    // If there is player ship1 missiles, call a collision checking for this actor with the list of the player Ship missiles
    if(Ships.missiles[0].length > 0){
      // If the collision checking return true
      if (Game.checkCollisionMissile(this.actor, Ships.missiles[0], this.amplitude)) {
        Sup.Audio.playSound('Sounds/shotContact');
[...]
Asteroid script
[...]
  die() {
    Sup.Audio.playSound('Sounds/explosion');
[...]
Ship script
[...]
  die() {
    Sup.Audio.playSound('Sounds/explosion');
  [...]
  update() {
    [...]
    // If the timer is 0 (!0 = true)
    if (!this.shootCooldown) {
      // If the shoot key is pressed
      if(Sup.Input.wasKeyJustPressed(Ships.commands[this.index].shoot)){
        Sup.Audio.playSound('Sounds/shipShot');
[...]

Our game is now complete and ready for release. We can still do whatever we want with it and transform it to do a new game, we will see that in the next chapter.

<-- back to chapter 9 -- go to chapter 11 -->