Wednesday, July 29, 2009

MJW Avoider Game ep8ch1



...That'd be episode 8, challenge 1

So now that he's shown us the magic of adding sound, he wants us to figure out how to create a Mute button on our own... sheesh. :) This was the easiest challenge, so far. But, so that I can solidify this in my own head, and in case you need help, here goes my thought process:

The first step I wanted to complete, had nothing (directly) to do with sound. I wanted Flash to recognize another button was being pressed. Instead of being original, I picked "m" as my mute button... Feel free to pick L, meaning "Let the music stop", or ß (ALT+0223) for "This music was picked for the beta. If you don't like it you can turn it off."

Going back and referencing my old code, I made these additions:

All of them are in the AvoiderGame.as file.

Create a publicly accessible variable (outside contructor)...
  1. public var mKeyIsBeingPressed:Boolean;)



Initialize it in the contructor...
  1. mKeyIsBeingPressed = false;



Inside the onKeyPress function
  1. if( keyboardEvent.keyCode == 77 )

  2. {

  3.     mKeyIsBeingPressed = true;

  4. }



Michael let you figure out the onKeyRelease function, and a student is not above his teacher, so I'll let you figure that out too.... :P

Then in the onTick function, I used a trace() command as a placeholder
  1. if( mKeyIsBeingPressed )

  2. {

  3.     trace( "You're pressing M!!!" );

  4. }



Don't forget to "Disable Keyboard Shortcuts"

...and then watch your Output window. When you press m, it should fill up with "You're pressing M!!!" over and over...



Ok, so it's working, I have a stream of useless output to prove it. Now I want it to actually do something useful! I created some new global variables:
  1. public var musicPosition:int;

  2. public var musicIsMuted:Boolean;


musicPosition is an integer. It's going to be equal to where the music was muted, so that when it's unmuted it'll play from the same spot. I guess that makes it a pause button... so, sue me.
musicIsMuted is straight forward. It's a true/false value of whether the music is muted.

...and then I initialized them, one in the constructor (musicIsMuted), and one in the onTick function
  1. musicIsMuted = false;


and
  1. if( mKeyIsBeingPressed )

  2. {

  3.     if( musicIsMuted )

  4.     {

  5.  

  6.     }

  7.     else

  8.     {

  9.         musicPosition = Math.floor(bgmSoundChannel.position);

  10.     }

  11. }



Ok, ok.... I did a little more then initialize it. I put together the basic if...else structure I was going to use. If the m key is being pressed, then if the music is already muted, do nothing right now, else check the position (in milliseconds) it was muted at, and put that into our variable, as a whole number (that's what Math.floor() does, if you don't remember)

Now, let's flesh it out some. If the music wasn't muted already, what do we want to do? We want to stop the background music from playing, and we want to change our variable (musicIsMuted) which tells us if the music is playing to true.
  1. else

  2. {

  3.     musicPosition = Math.floor(bgmSoundChannel.position);

  4.     bgmSoundChannel.stop();

  5.     musicIsMuted = true;

  6. }



If the player already muted the music, and now wants it to play again (cause they miss those jazzy synth beats!) then we would want it to play from where it stopped (we can do that by passing the play() function an argument), and change the musicIsMuted variable back to false.
  1. if( musicIsMuted )

  2. {

  3.     bgmSoundChannel = backgroundMusic.play( musicPosition );

  4.     musicIsMuted = false;

  5. }



Now save everything and test your game... Easy, huh?
Oh, wait, you say it keeps rapidly turning off and on? Crud! Well, we can fix that using some tidbits from Michael's sh'm'up tutorial.

Create another global variable:
  1. public var ticksSinceMutePressed:int;



Initialize it in the constructor:
  1. ticksSinceMutePressed = 0;



and change your function to:
  1. if( mKeyIsBeingPressed )

  2. {

  3.     if( ticksSinceMutePressed > 10 )

  4.     {

  5.         if( musicIsMuted )

  6.         {

  7.             bgmSoundChannel = backgroundMusic.play( musicPosition );

  8.             musicIsMuted = false;

  9.         }

  10.         else

  11.         {

  12.             musicPosition = Math.floor(bgmSoundChannel.position);

  13.             bgmSoundChannel.stop();

  14.             musicIsMuted = true;

  15.         }

  16.     ticksSinceMutePressed = 0;

  17.     }

  18. }

  19. ticksSinceMutePressed += 1;



If you haven't read that tutorial already, and can't piece together what I did, head over and check it out. No use in me re-inventing the wheel.

Avoider Game Shmup

That should do it. Now you can hear the 'thump, thump, thump' of the enemys coming on screen without that pesky synth-music playing in the background!

2 comments:

  1. Hahaha, I laughed at your suggestion of alternative keys :)

    Cool tutorial! Are we going to get to see your full game some time?

    ReplyDelete
  2. Thanks! Yeah, it's not that polished... but I'll post it!

    ReplyDelete