0

I've got an array of Strings each of which have been input by a series of coloured buttons with sounds attached. When a play button is pressed the strings that correspond to different coloured sounds should play one after the other. Instead they all play at the same time as one jumbled mess of noise and I have tried everything to get it to work. The trace shows that the for loop through the array seems to be doing the right thing which is why I can't understand why it won't just play back one after the other.

This is the code for the play button

enter code herePlayBTN.onRelease = function(){

trace("PlayButton Pressed");


for(var i:Number =0; i<songArray.length; ++i){


    trace("Inside Loop");
    trace(i);


    if(songArray[i]=="Orange"){

        OrangeSound.start(0,1);
        trace("Playing ORANGE Sound");
        //var interval = setInterval(wait,5000);
        //clearInterval(interval);
    }
    else if(songArray[i]=="Pink"){

        PinkSound.start(0,1);
        trace("Playing PINK sound");
    }
    else if(songArray[i]=="Yellow"){
        YellowSound.start(0,1);
        trace("Playing YELLOW Sound");
    }
    else if(songArray[i]=="Blue"){
        BlueSound.start(0,1);
        trace("Playing BLUE Sound");


    }
    else if(songArray[i]=="Green"){
        GreenSound.start(0,1);
        trace("Playing GREEN Sound");


    }
    else if(songArray[i]=="Red"){
        RedSound.start(0,1);
        trace("Playing RED Sound");

    }



}

This is how the sounds (words corresponding to sounds) are put in the array:

OrangeButton.onRelease = function(){ if(FreeIndex<10){ OrangeSound = new Sound();//When coloured buttons are pressed they make the sound that corresponds in the library OrangeSound.attachSound("OrangeSound");//the name of the sound in the library OrangeSound.start(0,1);//They only make the sound for one loop songArray[FreeIndex] ="Orange";

(Each colour having its own block of code similar to this.

Please help!

1 Answer 1

1

You would have to check that the sound has finished before playing the next sound.

function play(i){
    switch(songArray[i])){
        case Orange:
            OrangeSound.start(0,1);
            OrangeSound.onSoundComplete= function(){
                play(i+1);
            }
            break;
        case Pink:
            PinkSound.start(0,1);
            PinkSound.onSoundComplete= function(){
                play(i+1);
            }
            break;
...
        default:
            if(i+1 < songArray.length) play(i+1);
            break;
    }
}

Sound.onSoundComplete is used to check for the completion of a sound. The above, when invoked with play(0), will try to play a sound according to the value of songArray[i]. After the song plays, it will invoke play(1) and so on until i reaches the end of songArray. Instead of a for loop, though, play() simply reinvokes itself until it reaches the end of songArray.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SO much... after a little bit of fiddling this worked... Couldn't get it to work with i in the () of the play function declaration though but got around it. Thanks Again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.