ForumsProgramming ForumMusic in games/Muting?

5 3392
rjbman
offline
rjbman
215 posts
Nomad

I'm just curious. How exactly would you do music? I use the ENTER_FRAME programming method. Would Event sounds work? And is there a way to mute the sound using AS3?

Any help on this would be greatly appreciated.

  • 5 Replies
plasmafish
offline
plasmafish
252 posts
Nomad

There is multiple ways to initialize music and mute it. You have to determine if you want to load the song with the .swf, or have it load externally after the .swf is fully loaded.

I use AS2.0 primarily so I don't have a copy/paste method for you.

gluesy
offline
gluesy
65 posts
Nomad

I wouldn't recommend putting all your game logic in a ENTER_FRAME event listener because when your came gets complex then you will have major lag issues.

Anyway here's a code you can place outside your enterframe. You can then call the playMusic() and stopMusic() fuctions from within your ENTER_FRAME.


var musicy:song = new song();
var channel:SoundChannel;
var musicIsOn:Boolean=true;

function stopMusic():void {
trace("stop"
channel.stop();
musicIsOn=false;
}

function playMusic():void {
trace(&quotlay"
channel=musicy.play();
channel.addEventListener(Event.SOUND_COMPLETE, repeat);
}


For this to work you have to have the music in your library then open its properties and check Export for actionscript box and the identifier named song. This code is only really for music and not sound effects.

You need to call the function playMusic() for anything to happen.

gluesy
offline
gluesy
65 posts
Nomad

Oh i forgot something else, see this line:

channel.addEventListener(Event.SOUND_COMPLETE, repeat);

When your music ends this will call a function called repeat, Only use this if you want you music to loop forever. If you don't then remove that line but if you do then add the following:

function repeat(evt:Event):void {
if (channel!=null) {
channel.removeEventListener(Event.SOUND_COMPLETE, repeat);
playMusic();
}
}

rjbman
offline
rjbman
215 posts
Nomad

That's exactly what I was looking for.. thanks a bunch!

tehQED
offline
tehQED
33 posts
Nomad

That really helps; music and sound effects are such touchy subjects in programming. Is there advice for changing the background music at will? Or just for generating sound effects? I would like to know.

Showing 1-5 of 5