【发布时间】:2017-11-16 17:40:57
【问题描述】:
我正在尝试制作一个 simon 游戏,我几乎完成了,但我遇到了一个问题。 在 simon 游戏中,当 simon 给您命令(由闪烁的灯和每个灯的音频表示)您需要记住并在之后重复时,每次闪烁(带有音频)到下一个闪烁之间会有短暂的延迟。
所以现在,我的 simon 正在发出声音,但它会同时发出声音,没有任何延迟。我尝试使用setIntarvel 和setTimeout,但它仍然一次播放所有音频。
由于添加闪烁不应该那么难,所以我一直坚持到最后。
然后我建立了一个定时器功能:
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 playAudio(btnSound) {
if (btnSound == "c") {
c.play();
}
if (btnSound == "g") {
g.play();
}
if (btnSound == "a") {
a.play();
}
if (btnSound == "d") {
d.play();
}
}
而这个函数负责游戏的逻辑:
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);
}
}
}
这是整个脚本以便更好地阅读:
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);
}
【问题讨论】:
-
可能相关,I answered this other question 前一阵子,是关于为这样的游戏排序动画。
-
eek...不要在您的游戏循环中自旋锁定...有一个每隔 5 毫秒运行的时间间隔,而是跟踪增量时间。或者只是使用大量的超时来推进游戏状态。除非您进行嵌入式编程,否则自旋锁定永远不会有用。
-
我在google上看了一点关于spin-lock的内容,如果我理解正确的话,这里的spin-lock就是timer()函数?
标签: javascript for-loop delay