0

Frog

I'm developing a flash game called Frog, at the moment my code for sound seems a bit much, I'd like to simplify it but can't see how. Any help would be appreciated.

var musicSC:SoundChannel = new SoundChannel();
var musicST:SoundTransform = new SoundTransform();
var musicS:Sound = new Sound();
var musicURLR:URLRequest = new URLRequest('audio/music.mp3');
var flySC:SoundChannel = new SoundChannel();
var flyST:SoundTransform = new SoundTransform();
var flyS:Sound = new Sound();
var flyURLR:URLRequest = new URLRequest('audio/fly.mp3');
var frogSC:SoundChannel = new SoundChannel();
var frogST:SoundTransform = new SoundTransform();
var frogS:Sound = new Sound();
var frogURLR:URLRequest = new URLRequest('audio/frog.mp3');

function loopMusic():void {
    musicSC = musicS.play();
    musicSC.addEventListener(Event.SOUND_COMPLETE, loopMusic);
}
function loopFrog():void {
    frogSC = frogS.play();
    frogSC.addEventListener(Event.SOUND_COMPLETE, loopFrog);
}
function playFly():void {
    flySC = flyS.play();
}

musicS.load(musicURLR);
flyS.load(flyURLR);
frogS.load(frogURLR);
loopMusic();
loopFrog();
Kristian Matthews
  • 800
  • 3
  • 14
  • 28

1 Answers1

0

The play method of the sound object has a second parameter for the number of loops:

musicS.play(0, int.MAX_VALUE);

int.MAX_VALUE should be enough loops for your app.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html

Matt Garland
  • 437
  • 1
  • 4
  • 11
  • Thank you, I'll use it since the int.MAX_VALUE is a ridiculously high number and I shouldn't need any more than that. However, purely for future reference, is there not a more efficient method for 'looping infinitely'? – Kristian Matthews Apr 27 '12 at 09:36
  • Also, should I be using multiple sound channels? I thought it would be the best method of being able to manipulate my sounds individually, is this correct? – Kristian Matthews Apr 27 '12 at 09:37
  • This is the most concise method, but it does have a drawback, which is that the looping is not seamless (Flash inserts a small silence at the beginning of mp3 that are loaded, though it loops perfectly if the sound is embedded in the IDE). See some solutions here if you can't live with the gap: http://forums.flixel.org/index.php?topic=2964.0. – Matt Garland Apr 27 '12 at 13:23
  • If you use the loop parameter, you do not need the sound channel, since you don't need to listen for the complete event. But if you want to stop and start sounds and otherwise control them for other reasons, you are definitely going to need the sound channel. – Matt Garland Apr 27 '12 at 13:25