Jump to content

Playing around with audio and movement.


Kadir khatri
 Share

Recommended Posts

I will be explaining audio using an example of a car. In this game you will be driving a car around the scene and as an when you press buttons there will be different audios that will be generated while the car will be moving around in the scene.

I will be explaining the code in parts

So firstly we will be having our preload function, in this function we will be adding our car image or animation and then loading our audio that are going to be used in the game.

 

preload: function ()
    {
       this.load.image('background',"assets/lesson.jpg");
       this.load.audio('cstart', 'assets/cstart.mp3');
       this.load.audio('move', 'assets/cstart.mp3');
       this.load.audio('stop', 'assets/bstart.mp3');
       this.load.image('car',"assets/back.jpg");

    },

 

Now in the create function  we will  be inserting the assets that we need during the start of the game i.e. the sound of car start the image of the car the background, the rest of the audio can be added when the condition is satisfied i.e. we will put that part in the update function with the movement.

create: function ()
    {
           start = this.physics.add.image(400, 300, 'background');
           start2 = this.physics.add.image(400, 300, 'car');
            ms = game.add.audio(‘stop');
            msc = game.add.audio(‘move');
            music = game.add.audio(‘cstart');
            music.play();
    }

 

Now moving to the update part, here we will be coding the movement part and with it the audio for movement.

update:function(){

if (cursors.right.isDown) 
 { 
     if (car.x !=600)  
   {
       car.x += 5;
       c_move.play();

    } 
 }
  if (cursors.left.isDown)
   {   
     if (car.x !=20)
     {       
         car.x += -5;  
         c_move.play();
    
     }  
   } 
  if (cursors.up.isDown)
   {    
      if (car.y !=20)
     {      
          car.y += -1;
          c_move.play();

      }   
   } 
if (cursors.down.isDown)
   {   
        if (car.y !=550)
     {   
         car.y += 1;
         c_move.play();

      } 
   }
if(car.x>590&&car.y<100)
     {	
         stop.play();
      }	};

In the above code we can see that when we move in any direction the move audio will be playing, when we move out of the scene a crash sound will be playing because we have put boundaries to the scene as a result going out of the boundaries will cause to make a crash sound.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...