vendredi 11 septembre 2015

Pausing execution for animation

I am making a simple Simon Says game for practice, but I am having a little trouble.

When the AI plays out the pattern that the user is to immitate, I want it to press the buttons one at a time. There needs to be a little pause between each button press, to allow for the sound and animation to complete.

I am storing the pattern in an array, and using a for loop to cycle through it, like this:

var computerPattern = [1, 2, 3, 4];

for (i=0; i<computerPattern.length; i++){
    setTimeout(function() {
        switch(computerPattern[i]) {
            case 1:
                    beep($("#green"));
                    break;
            case 2:
                    beep($("#red"));
                    break;
            case 3:
                    beep($("#blue"));
                    break;
            case 4:
                    beep($("#yellow"));
                    break;
            default:
                    break;
        }
    }, 250);
}
// Where 'beep' is a function that plays a sound and animation.

As you can see, I am using setTimeout because that's what Ive been able to find throught my research. But it's not working, so maybe my whole approach is wrong.

I would appreciate any suggestions as to how to go about this. Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire