Lunar Lander SOUND OPTION

In this version of the game, you will be adding sound to your game.




Add our sound data files
Add this code to the end of the PRELOAD function of the MainMenuState.
This code will add all the sound files we need in our game.


                 
                 
    // rocket engine sound
    game.load.audio('engine-sound', 'https://res.cloudinary.com/smoothdaddyg/video/upload/v1512924751/233489__aleixcm__lighter_hzxin8.wav');
    
    // explosion sound
    game.load.audio('explosion-sound', 'https://res.cloudinary.com/smoothdaddyg/video/upload/v1512919930/399303__dogfishkid__explosion-012_v7ls75.mp3');
  
    // landing success sound
    game.load.audio('landing-sound', 'https://res.cloudinary.com/smoothdaddyg/video/upload/v1512921203/171671__fins__success-1_wksyfd.wav');
  
    // game music sound
    game.load.audio('music-sound', 'https://res.cloudinary.com/smoothdaddyg/video/upload/v1512920558/172561__djgriffin__video-game-7_hqrbzf.wav');
  
  
        

Run Your Code and check for errors.
You won't hear anything yet.
Remember to press F12 to open the Developer Tools, click Console tab and make sure there are no errors.





Game Music Sound
Add this code to the end o fthe CREATE function of the Level1State. This code will add some background music to our game.



   // play some music during our game
   // ----------------------------------------------------------
   
   // create our sound object
   this.MainMenuSound = game.add.audio('music-sound');
   
   // play sound over and over at 100% volume
   this.MainMenuSound.loopFull(1);
   
   // play it
   this.MainMenuSound.play(); 

              
        

Next, we need to Add this new function after the CREATE function of the Level1State. This code will destroy our music object when we end this state.


                 
  shutdown: function() { 
      this.MainMenuSound.destroy();
  },
              
        

Run Your Code and check for errors.
Play the game! You should hear some background gaming music!


Create object to manage engine sound
Add this code to the end of the CREATE function of the Level1State. This code will create our object we will use to manage our engine sound.


     
    // adds an object to our game to control engine sound
    this.EngineSound = game.add.audio('engine-sound');
                
        

Play engine sound
In the UPDATE function of the Level1State there are three IF statements which turn the engines on. Add this line of code to the end of each IF statement.


     
        this.EngineSound.play();
                
        

for example...the UP engine IF statement might look like this after the 'sound' line of code is added.


     
    // check if down engines are on
    if (this.keyboard.isDown(Phaser.Keyboard.UP) && this.FuelTank > 0) { 

      // up arrow is being pressed, so show image with down thruster on
      this.rocket.loadTexture('rocketEngineDownOn', 0);

      // increase current velocity by 10
      this.rocket.body.velocity.y += -5; 
      
      // rocket engine is on 
      RocketEngineOff = false;
      
      this.EngineSound.play();

    }
                
        

Run Your Code and check for errors.
Press the arrow keys to fire the rockets and see if u hear any sounds.





Add sound to landing
Find the touchDown function in the Level1State. We will need to add the following lines of code to the end of each part of the IF statement depending if it's a perfect touchdown or hard touchdown.


     
        // landing sound
        this.LandingSound = game.add.audio('landing-sound');
        this.LandingSound.play();
        
            
        // explosion sound
        this.LandingSound = game.add.audio('explosion-sound');
        this.LandingSound.play();

        

Your new TouchDown function might look like this.


     
                   touchDown: function(_rocket, _pad_a) {

    // check if the rocket touched down softly
    if (_rocket.body.velocity.y < 80) { 

        // stop all rocket motion
        _rocket.body.enable = false;
        
        // display game over and instructions
        game.add.text(50,30,'PERFECT TOUCH DOWN!!', {font: '50px Arial', fill: '#02f20e' });
        
        // display game over and instructions
        game.add.text(50,100,'Restart? \npress spacebar', {font: '40px Arial', fill: '#ffffff' });

        // this level is over
        Level1State.ThisLevelIsOverDude = true;

        // landing sound
        this.LandingSound = game.add.audio('landing-sound');
        this.LandingSound.play();
        
    } else {
     
        // stop all rocket motion
        _rocket.body.enable = false;
      
        // display game over and instructions
        game.add.text(50,100,'LANDING TOO HARD! GAME OVER \n Restart? \n press spacebar', {font: '40px Arial', fill: '#ffffff' });
      
        // this level is over
        Level1State.ThisLevelIsOverDude = true;

        // explosion sound
        this.LandingSound = game.add.audio('explosion-sound');
        this.LandingSound.play();

    }

  },

        
Run Your Code and check for errors.
Try landing hard and soft to hear the difference.





Add explosion sound when hitting ground
Add this code to the of the hitGround function in the Level1State.



    // explosion sound
    this.LandingSound = game.add.audio('explosion-sound');
    this.LandingSound.play();    

        
Run Your Code and check for errors.
Try hitting the ground to hear explosion.





Add explosion sound when hitting ground
Add this code to the of the hitGround function in the Level1State.



    // explosion sound
    this.LandingSound = game.add.audio('explosion-sound');
    this.LandingSound.play();    

        
Run Your Code and check for errors.
Try hitting the ground to hear explosion.


GameOver explosion
Add this code to the of the gameOver function in the Level1State.



    // explosion sound
    this.LandingSound = game.add.audio('explosion-sound');
    this.LandingSound.play();    

        
Run Your Code and check for errors.
Try flying off the screen to hear explosion.


You're done! You are a coder!!
What other things can you add to this game?
What other games can you make up using this code!?