Hi everybody
I am busy with the Simon game and having some problems.
I have an array that holds the sequence for the computer. Now I am trying to iterate through the computer array at a pace of one index every 1.5 second and at every index I want the corresponding sound to play. With the code I am using (below) all the sounds associated with every index plays at the same time.
function sequence(computerArr) {
if (computerArr.length === count) {
var interval = setInterval(function(){
for (var i = 0; i < computerArr.length; i++) {
if (computerArr[i] === 0) {
soundRed.load();
soundRed.play();
clearInterval(interval);
} else if (computerArr[i] === 1) {
soundBlue.load();
soundBlue.play();
clearInterval(interval);
} else if (computerArr[i] === 2) {
soundGreen.load();
soundGreen.play();
clearInterval(interval);
} else if (computerArr[i] === 3) {
soundYellow.load();
soundYellow.play();
clearInterval(interval);
}
}
}, 1500);
}
};
I am guessing the way I have it, is that every 1.5 seconds the whole for loop is executed, and that is why all the sounds of all the indices play together.
How can I iterate through the array one index at a time every 1.5 seconds?