Tengo este pequeño juego de Pacman hecho en JavaScript.
Lo que sucede es que cuando llamo a un elemento aleatorio de la matriz, esto es lo que devuelve el juego: Video aquí
El juego muestra todos los elementos de la matriz, antes de mostrar solo uno de ellos...
Una parte del código
if (gameOver || gameWin) { //display this array if game win text = textOptionsWin[Math.floor(Math.random() * textOptionsWin.length)];; if (gameOver) { //display this array if game over text = textOptions[Math.floor(Math.random() * textOptions.length)];; //go back to the first level if lost setTimeout(function () { if (tileMap.fase == 1) { location.href = "/?fase=1"; // text = "O COVID-19 TE PEGOU! 😷"; } else if (tileMap.fase == 2) { location.href = "/?fase=1"; } else if (tileMap.fase == 3) { location.href = "/?fase=1"; } }, 2500); //Delay to change the level } }Me falta algo para que esto suceda? cualquier tip puede ser util
No entiendo completamente, pero creo que esta es la respuesta:
var a = ["end1", "end2", "end3"] console.log(a[Math.floor(Math.random() * 3)])Esto imprime un elemento aleatorio en la matriz 'a' en la consola.