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?
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.
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("lay" 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.
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(); } }
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.