I’m trying to get my audio to sound when a random button is selected by Simon, or even when the user presses it, but it is not working! Thanks in advance for your help!
//tie this if function into start button to enable random button to be selected by Simon game and audio played
if(buttons.length > 0) { //as long as greater than 0, can be two buttons, etc
function getRandomIndex(){
var random = Math.floor(Math.random() * 4);
buttons.item(random).click(); //randomIndex is parameter of item
simonGamePlays.push(random);
if(random[i]==1){
lightup = 'hit-zero-green';
$('.hit-zero-green').addClass(lightup);
$('#audio1')[0].play();
simonGamePlays.push(1);
setTimeout(function () {
$('.hit-zero-green').removeClass(lightup);
}, off);
}else if (random[i] == 2) {
lightup = 'hit-zero-red';
$('.hit-zero-red').addClass(lightup);
$('#audio-two')[0].play();
simonGamePlays.push(2);
setTimeout(function () {
$('.hit-zero-red').removeClass(lightup);
}, off);
}else if (random[i] == 3) {
lightup = 'hit-zero-blue';
$('.hit-zero-blue').addClass(lightup);
$('#audio3')[0].play();
simonGamePlays.push(3);
setTimeout(function () {
$('.hit-zero-blue').removeClass(lightup);
}, off);
}else if (random[i] == 3) {
lightup = 'hit-zero-yellow';
$('.hit-zero-yellow').addClass(lightup);
$('#audio4')[0].play();
simonGamePlays.push(4);
setTimeout(function () {
$('#sound-four').removeClass(lightup);
}, off);
}
}
//outside function
getRandomIndex(); // function call
}
} //end of turn game on function
Hey - I don’t know if this is all of the issue but this might be a part of it…in your HTML where you have the audio and src tags. It’s actually supposed to be like this:
//tie this if function into start button to enable random button to be selected by Simon game and audio played
if(buttons.length > 0) { //as long as greater than 0, can be two buttons, etc
function getRandomIndex(){
var random = Math.floor(Math.random() * [i]);
buttons.item(random).click(); //randomIndex is parameter of item
simonGamePlays.push(random);
if(random==1){
lightup = 'hit-zero-green';
$('.hit-zero-green').addClass(lightup);
$('#audio1')[0].play();
simonGamePlays.push(1);
setTimeout(function () {
$('.hit-zero-green').removeClass(lightup);
}, off);
}else if (random== 2) {
lightup = 'hit-zero-red';
$('.hit-zero-red').addClass(lightup);
$('#audio2')[0].play();
simonGamePlays.push(2);
setTimeout(function () {
$('.hit-zero-red').removeClass(lightup);
}, off);
}else if (random == 3) {
lightup = 'hit-zero-blue';
$('.hit-zero-blue').addClass(lightup);
$('#audio3')[0].play();
simonGamePlays.push(3);
setTimeout(function () {
$('.hit-zero-blue').removeClass(lightup);
}, off);
}else if (random== 4) {
lightup = 'hit-zero-yellow';
$('.hit-zero-yellow').addClass(lightup);
$('#audio4')[0].play();
simonGamePlays.push(4);
setTimeout(function () {
$('#sound-four').removeClass(lightup);
}, off);
}
}
//outside function
getRandomIndex(); // function call
}
} //end of turn game on function
This doesn't work.....when I hit **start**, it only selects the green button lol.
And yes, I know I have some other logic issues, but I believe I can atleast get the sound to work first lol
Also, about the design, interesting point, I'll think about it...
@camperextraordinaire, I previously re-wrote it to if(random==1), and if(random==2), and if(random==3), and if(random==4), but it was causing a button to disappear…so I changed it back…
//tie this if function into start button to enable random button to be selected by Simon game and audio played
if(buttons.length > 0) { //as long as greater than 0, can be two buttons, etc
function getRandomIndex(){
var random = Math.floor(Math.random() * 4);
buttons.item(random).click(); //randomIndex is parameter of item
simonGamePlays.push(random);
if(random ==1){
lightup = 'hit-zero-green';
$('.hit-zero-green').addClass(lightup);
$('#audio1')[0].play();
simonGamePlays.push(1);
setTimeout(function () {
$('.hit-zero-green').removeClass(lightup);
}, off);
}else if (random == 2) {
lightup = 'hit-zero-red';
$('.hit-zero-red').addClass(lightup);
$('#audio2')[0].play();
simonGamePlays.push(2);
setTimeout(function () {
$('.hit-zero-red').removeClass(lightup);
}, off);
}else if (random == 3) {
lightup = 'hit-zero-blue';
$('.hit-zero-blue').addClass(lightup);
$('#audio3')[0].play();
simonGamePlays.push(3);
setTimeout(function () {
$('.hit-zero-blue').removeClass(lightup);
}, off);
}else if (random == 4) {
lightup = 'hit-zero-yellow';
$('.hit-zero-yellow').addClass(lightup);
$('#audio4')[0].play();
simonGamePlays.push(4);
setTimeout(function () {
$('#sound-four').removeClass(lightup);
}, off);
}
}
//outside function
getRandomIndex(); // function call
}
} //end of turn game on function