Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中修改变量?不起作用_Javascript_Variables_Global_Local_Pacman - Fatal编程技术网

在javascript中修改变量?不起作用

在javascript中修改变量?不起作用,javascript,variables,global,local,pacman,Javascript,Variables,Global,Local,Pacman,我正试图让它,当吃豆人遇到鬼,他失去了生命。然后根据他生命的数量显示一条特定的信息,但是变量似乎没有考虑到我正在改变它 有什么建议吗 var life=3; // Lorsque l'on rencontre un fantome. function manchePerdue() { x_Pacman = 13; y_Pacman = 10; life = life-1; if (life=2) { window.alert("2 Lives Left"); }

我正试图让它,当吃豆人遇到鬼,他失去了生命。然后根据他生命的数量显示一条特定的信息,但是变量似乎没有考虑到我正在改变它

有什么建议吗

var life=3;
// Lorsque l'on rencontre un fantome.
function manchePerdue()
{ 
  x_Pacman = 13;
  y_Pacman = 10;

  life = life-1;

  if (life=2) {
    window.alert("2 Lives Left");
  }
  else if (life=1) {
    window.alert("1 Life Left");
  }
  else { 
    window.alert("Game Over");
    gameover();
  }
}

您需要在if中使用相等运算符
=
,而不是赋值运算符
=

if (life == 2) {
  alert("2 Lives Left");
else if (life == 1) {
  alert("1 Life Left");
} else {
  alert("Game Over");
  gameover();
}
使用
=
意味着您正在重新分配每个中的值以进行检查。例如:

var lives = 3;
if (lives = 1) {
  alert("1 life"); // Will show up
}
alert(lives); // Will show "1"!
Javascript还具有使用
==
运算符严格相等的思想。在严格相等的情况下,类型也必须匹配

1 == 1; // true
1 == "1"; // true
1 === "1"; // false

值得一提的是,您还注意到不需要编写
窗口。alert
,只需编写
alert
就足够了,因为
窗口
是全局范围。

您需要在if's中使用相等运算符
=
,而不是赋值运算符
=

if (life == 2) {
  alert("2 Lives Left");
else if (life == 1) {
  alert("1 Life Left");
} else {
  alert("Game Over");
  gameover();
}
使用
=
意味着您正在重新分配每个中的值以进行检查。例如:

var lives = 3;
if (lives = 1) {
  alert("1 life"); // Will show up
}
alert(lives); // Will show "1"!
Javascript还具有使用
==
运算符严格相等的思想。在严格相等的情况下,类型也必须匹配

1 == 1; // true
1 == "1"; // true
1 === "1"; // false

值得一提的是,您还注意到不需要编写
窗口。alert
,只需编写
alert
就足够了,因为
窗口
是全局范围。

您需要在if's中使用相等运算符
=
,而不是赋值运算符
=

if (life == 2) {
  alert("2 Lives Left");
else if (life == 1) {
  alert("1 Life Left");
} else {
  alert("Game Over");
  gameover();
}
使用
=
意味着您正在重新分配每个中的值以进行检查。例如:

var lives = 3;
if (lives = 1) {
  alert("1 life"); // Will show up
}
alert(lives); // Will show "1"!
Javascript还具有使用
==
运算符严格相等的思想。在严格相等的情况下,类型也必须匹配

1 == 1; // true
1 == "1"; // true
1 === "1"; // false

值得一提的是,您还注意到不需要编写
窗口。alert
,只需编写
alert
就足够了,因为
窗口
是全局范围。

您需要在if's中使用相等运算符
=
,而不是赋值运算符
=

if (life == 2) {
  alert("2 Lives Left");
else if (life == 1) {
  alert("1 Life Left");
} else {
  alert("Game Over");
  gameover();
}
使用
=
意味着您正在重新分配每个中的值以进行检查。例如:

var lives = 3;
if (lives = 1) {
  alert("1 life"); // Will show up
}
alert(lives); // Will show "1"!
Javascript还具有使用
==
运算符严格相等的思想。在严格相等的情况下,类型也必须匹配

1 == 1; // true
1 == "1"; // true
1 === "1"; // false

值得一提的是,你也会注意到你不需要写
窗口。alert
,只写
alert
就足够了,因为
窗口
是全局范围。

=
是赋值
=
是相等的,检查你的if语句。
=
是赋值
=
是相等的,检查您的if语句。
=
是否赋值
=
是否相等,请检查您的if语句。
=
是否赋值
=
是否相等,请检查您的if语句。