小编Slu*_*ego的帖子

1 到 4 之间的一个随机数,它不是另一个随机数

由于某种原因,以下代码不起作用。

var a1 = Math.floor(Math.random()*4+1); 

//Answer2
for(a2 = 0; a2 != a1 && a2 != 0; a2 = Math.floor(Math.random()*4+1)){
    alert(a2);
}
Run Code Online (Sandbox Code Playgroud)

我试图让“a2”成为 1-4 的整数,但不等同于“a1”。

怎么了?提前致谢!!!

编辑:

谢谢大家的帮助!这是我的最终结果:

var a1, a2;
a1 = Math.floor(Math.random()*4+1); 
a2 = a1;

while(a2 == a1){
        a2 = Math.floor(Math.random() * 3 + 1);
        alert(a2);
}
Run Code Online (Sandbox Code Playgroud)

javascript random math for-loop

1
推荐指数
1
解决办法
4744
查看次数

我需要帮助在我的JavaScript迷你游戏中找到错误(我没有得到的三个错误)

这是我第一次从头开始制作迷你游戏.

谷歌浏览器在运行时给我这些错误:

Uncaught TypeError: Cannot call method 'draw' of undefined  Logic.js:28
loop                                                        Logic.js:28
startLoop                                                   Logic.js:35
init                                                        Logic.js:19
Run Code Online (Sandbox Code Playgroud)

当我使用"setInterval"时它工作正常,但我想要最新的东西.我不认为requestAnimationFrame与它有任何关系.

但我没有看到什么是错的!请帮忙.

// Create the canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.addEventListener('click', canvasClick, false);

//Declarations
var isPlaying = false;
var animeFrame = window.requestAnimationFrame ||
                 window.webkitRequestAnimationFrame ||
                 window.mozRequestAnimationFrame ||
                 window.msRequestAnimationFrame ||
                 window.oRequestAnimationFrame;

var Pair1;
var Pair2;

//init
function init(){
    startLoop();
    Pair1 = new Pair();
    Pair2 = new Pair();
    alert('init called');
}

//Draw
function loop(){
    if(isPlaying){
        Pair1.draw();
        Pair2.draw();
        animeFrame(loop);
    }
}
function …
Run Code Online (Sandbox Code Playgroud)

javascript html5 google-chrome

0
推荐指数
1
解决办法
83
查看次数

标签 统计

javascript ×2

for-loop ×1

google-chrome ×1

html5 ×1

math ×1

random ×1