我正在尝试制作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);
}
因此,您想在每个playAudio通话后暂停一下吗?
我还是会超时
var sounds = {};
sounds.a = ...;
sounds.b = ...;
function playAudio(sound, callback) {
if (typeof sounds[sound] === "undefined") {
throw "sound not available";
}
sounds[sound].play();
if (typeof callback !== "undefined") {
window.setTimeout(callback, 500);
}
}
// play a - pause - b - pause c
playAudio("a", function(){
playAudio("b", function() {
playAudio("c");
});
});
如果您先构建序列,那么也可以动态进行
var sequence = ["a", "b", "c"];
var pointer = 0;
playAudio(sequene[pointer], function() {
pointer += 1;
if (typeof sequence[pointer] !== "undefined") {
playAudio(sequence[pointer], this); // this will be bound to the function itself
} else {
alert("sequence finished");
}
});
这未经测试,但可以正常工作,如果没有,请通知我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句