[英]variable value changing does not delay setTimeout?
I am making a red light green light type game.我正在制作一个红灯绿灯类型的游戏。 Here is the part where the stoplight changes color:这是红绿灯变色的部分:
var green = function() {
var r = Math.floor(Math.random() *5000 + 3000);
ctx.fillStyle = "green";
ctx.fillRect(900, 580, 200, 100);
green = true;
setTimeout(red, r);
};
var red = function() {
var r = Math.floor(Math.random() *3000 + 2000);
ctx.fillStyle = "red";
ctx.fillRect(900, 580, 200, 100);
green = false;
setTimeout(green, r);
};
If I take out the variable green, then it works fine but I need it so the game knows when the player can and cant move.如果我取出变量 green,那么它工作正常,但我需要它以便游戏知道玩家何时可以移动和不能移动。 I would appreciate any help!我将不胜感激任何帮助!
You override the variable green
.您覆盖了变量green
。
Initially it is a function
, and inside the red
function, you assign true
to it.最初它是一个function
,在red
函数中,您为其分配true
。 That's the reason your setTimeout(green,r);
这就是你setTimeout(green,r);
的原因setTimeout(green,r);
doesn't work.不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.