Monday 1 February 2010

Flash Game: Sounds and that annoying outside element!

So as the title says I will discussing my sounds and how I got them working. I decided that my sounds HAD to be car and driving based and what 2 better sounds could I get than Jessica by the Allman Brothers (or as its better known the Top Gear theme) and The Chain by Fleetwood Mac (or as its better known the formula 1 theme - not sure if its the old or current but oh well).
So after gaining these mp3 files I set about coding getting them to play.

var snd:Sound = new Sound();
snd.load(new URLRequest("TheChainSample.mp3"));
var channel:SoundChannel = new SoundChannel();

channel = snd.play();

function playGame(event:MouseEvent){
gotoAndPlay(3);
channel.stop();
}


The code you can see is from my intro frame. It calls the sound file (snd.load) and then places it into the var channel. I then tell the channel to play but stop once the user clicks the play button. The reason being that I want a new sound to play during the game itself.

var snd2:Sound = new Sound();
snd2.load(new URLRequest("JessicaSample.mp3"));
var channel2:SoundChannel = new SoundChannel();

channel2 = snd2.play();

channel2.stop();


This is the code used on my actual game frame. Once again I call the file (snd2.load) and place it into a var (this time channel2) and I then tell it to play (snd2.play). I had to add the 2's on the end of snd and channel otherwise it would play the sound from the intro screen again. The stop code you saw previously in this post. I tell the sound to stop once the player returns to the splash screen.


Now I will go onto the external element part of the brief that we had to include. I decided to keep it simple and to change the background depending on what hour of the day it was. If it was an odd number the background of my track would be brown however if it was an even number the background would remain green. I did it as so:

var currentDate:Date = new Date();
trace(currentDate.getHours());

I called the current date and used it to get the current hour of the day.

I then created a function and within it typed:
function changeBackground(evt:Event):void
{
if ((currentDate.getHours() == 1)||(currentDate.getHours() == 3)||(currentDate.getHours() == 5)||(currentDate.getHours() == 7)||(currentDate.getHours() == 9)||(currentDate.getHours() == 11)||(currentDate.getHours() == 13)||(currentDate.getHours() == 15)||(currentDate.getHours() == 17)||(currentDate.getHours() == 19)||(currentDate.getHours() == 21)||(currentDate.getHours() == 23)){
mc_newBG.x = 400;
mc_newBG.y = 400;
mc_newinsideBG.x = 76.40
mc_newinsideBG.y = 90.95
}
}

Unfortunately the block quote ruins the lay out. Basically I created two off screen brown objects and moved them inscreen whenever the current hour was 1,3,5,7,9,11,13,15,17,19,21 or 23. The inside of the track was difficult to do. I had to set the registration to the top left corner, grabbed the co-ordinates of my inside track rectangle and then set the brown rectangle to move into the exact same space.

And that my friends is my flash game finished! All thats left for you to do is to go and play it which you can do so HERE. Goodbye for now.

No comments:

Post a Comment