变量不耗尽javascript [英] Variable not depleting javascript

查看:66
本文介绍了变量不耗尽javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我正在创造一个你可以买口袋妖怪的游戏。当购买宠物小精灵时,我显然希望硬币变量可以通过口袋妖怪花费的硬币数来减少。但是,当我这样做时,硬币变量总是保持在1000并且当我购买宠物小精灵时不会掉落。这是按下按钮时使用的功能:



 function buymagikarp(){
if(coins> = 100){
swal(成功,你买了1个magikarp!,成功);
币 - 100;
pokemonchosen.push(Magikarp);
} else {
swal(错误,余额不足,错误);
}
}





我的尝试:



我试图删除swal()以查看警报是否弄乱了硬币变量。

解决方案

币 - 100 只计算值,但从不存储它。你的意思是:

币 -  =  100 ;  //  (相当于:)币=硬币 -  100;  


Hello! I am creating a game where you can buy pokemon. When the pokemon is bought, I obviously want the coins variable to drop by the # of coins the pokemon costs. However, when I do this, the coins variable always stays at 1000 and does not drop when I buy the pokemon. Here is the function that is used when onclick on a button:

function buymagikarp() {
  if(coins >= 100 ) {
    swal("Success", "You bought 1 magikarp!", "success");
    coins - 100;
    pokemonchosen.push("Magikarp");
  } else {
    swal("Error", "Not enough balance", "error");
  }
}



What I have tried:

I have tried to remove the swal("") to see if the alerts were messing up the coins variable.

解决方案

coins - 100 just calculates the value, but never stores it. Did you mean:

coins -= 100; // (equivalent of:) coins = coins - 100;

?


这篇关于变量不耗尽javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆