I'm trying to make a simon game and I'm almost done but I got an issue. In a simon game, when the simon's giving you the order (which represented by blinking lights and audio for each light) which you need to remember and repeat afterwards, there's a short delay between each blink (with it's audio) to the next one.
So now, my simon is making sounds but it's doing them all at once, with no delay. I tried using setIntarvel
and setTimeout
but still it played all audios at once.
Since adding the blinking shouldn't be that tough I'm keeping it to the end.
Then I built a timer function:
function timer(delayInMS) {
var time = new Date();
var currentTime = time.getTime();
var nextTime = time.getTime() + delayInMS;
while (currentTime != nextTime) {
time = new Date();
currentTime = time.getTime();
}
}
And used it inside the function which playing the audios, but it's still doing the same thing - playing it all at once.
This is the function which responsible for the audios:
function playAudio(btnSound) {
if (btnSound == "c") {
c.play();
}
if (btnSound == "g") {
g.play();
}
if (btnSound == "a") {
a.play();
}
if (btnSound == "d") {
d.play();
}
}
And this function responsible for the logic of the game:
var btns = ["c", "g", "a", "d"];
var randomOrder = "";
var playerInputOrder = "";
var timer = 1000; //For timer()
function randomSimon() {
var randomBtn = btns[Math.floor(Math.random() * 4)];
randomOrder += randomBtn;
for (i = 0; i <= randomOrder.length - 1; i++) {
for (s = 0; s <= i; s++) {
var someText = "";
someText += randomOrder.charAt(s);
playAudio(randomOrder.charAt(s));
document.getElementById('debug').innerHTML = someText; //this is for debugin, should be ignored.
timer(500);
}
}
}
And this is the whole script for better reading:
var isGameStarted = false;
var d = new Audio("dSharpNote.wav");
var a = new Audio("aSharpNote.wav");
var g = new Audio("gSharpNote.wav");
var c = new Audio("cNote.wav");
var btns = ["c", "g", "a", "d"];
var randomOrder = "";
var playerInputOrder = "";
var timer = 1000;
function startGame() {
isGameStarted = true; //So players won't be able to just press the simon's buttons.
randomOrder = ""; //A variable to keep the random order given by the simon.
playerInputOrder = ""; //A variable to keep the random order the player inputs.
randomSimon(); //Called to give the first button in the order.
}
function randomSimon() { //Adds one random button to the order and saves the order.
var randomBtn = btns[Math.floor(Math.random() * 4)]; //Adds the random button.
randomOrder += randomBtn; //Saves the random order.
for (i = 0; i <= randomOrder.length - 1; i++) { //this should play all the audios one by one according the saved order
for (s = 0; s <= i; s++) {
var someText = "";
someText += randomOrder.charAt(s);
playAudio(randomOrder.charAt(s));
document.getElementById('debug').innerHTML = someText; //this is for debugin, should be ignored.
timer(500);
}
}
}
function timer(delayInMS) {
var time = new Date();
var currentTime = time.getTime();
var nextTime = time.getTime() + delayInMS;
while (currentTime != nextTime) {
time = new Date();
currentTime = time.getTime();
}
}
function playerProgress(btn) { //Getting the player's input and checks if it's in the right order.
if (isGameStarted == true) {
var btnClicked = btn.id; //Gets the id of the button the player clicked.
playAudio(btnClicked); //So this could play it's audio.
playerInputOrder += btnClicked;
if (playerInputOrder.length == randomOrder.length) {
if (playerInputOrder == randomOrder) {
playerInputOrder = "";
setTimeout(randomSimon, 1000);
} else {
document.getElementById('scoreText').innerHTML = randomOrder + " Game Over!";
isGameStarted = false; //Makes the player unable to play the simon's button's audios.
}
}
document.getElementById('debug').innerHTML = playerInputOrder;
}
}
function playAudio(btnSound) {
if (btnSound == "c") {
c.play();
}
if (btnSound == "g") {
g.play();
}
if (btnSound == "a") {
a.play();
}
if (btnSound == "d") {
d.play();
}
}
function someInterval() {
var i = 0;
var anInterval = setInterval(function() {
i++;
if (i > 11) {
clearInterval(anInterval);
timer *= 2;
}
}, timer);
}