简体   繁体   English

虚拟赌场中的“货币”货币变量不起作用,立即重置

[英]“Currency” currency variable in virtual casino not working, instantly resets

I'm trying to make a virtual casino to practice with Javascript. 我正在尝试制作一个虚拟赌场来练习Javascript。 I already got the roulette wheel working, so now I wanted to add a "currency" feature. 我已经可以使用轮盘赌了,所以现在我想添加一个“货币”功能。 Basically, the player enters the casino with 200 dollars, and as he or she gambles, their currency amount changes. 基本上,玩家带着200美元进入赌场,随着他或她的赌博,他们的货币金额会发生变化。

Here's what I tried to do. 这是我尝试做的。 In the html part of the document, there is a sentence that reads "You currently have $200." 在文档的html部分中,有一句显示为“您当前有200美元”。 The 200 is enclosed in a div called "m." 200包含在称为“ m”的div中。 The variable "money" then takes this number from that sentence/div. 然后,变量“ money”从该句子/ div中获取此数字。 When the player spins the roulette wheel, money is subtracted or added to the variable money and then the contents of the div "m" is changed accordingly. 当玩家旋转轮盘赌时,钱会减去或加到变量钱中,然后div“ m”的内容会相应更改。

What I'd hoped is that, after the player spun the wheel, that would rewrite the contents of div 'm', which would in turn change the variable "money." 我希望的是,在玩家旋转方向盘之后,它将重写div'm'的内容,从而改变变量“ money”。 However, this just doesn't work. 但是,这根本行不通。 Here's a sample of the code: 这是代码示例:

var money = document.getElementById("m");

function spin(){
  var landing = Math.floor(Math.random() * 4) + 1;   

  var nsvalue = parseInt(document.regular_roulette.num_select.value);

  var betvalue = parseInt(document.regular_roulette.bet.value);

  if (landing === nsvalue) {
    alert("You win $" + betvalue * 3);

    money = money + (betvalue * 3);
  } else {
    alert("You lose $" + betvalue);

    money = money - betvalue;
  }

  document.getElementById("m").innerText = money;
}

//why is it fleeting?  I've seen this before.
        </script>
</head>
<body>

    You currently have $<div id = "m">200</div>

Let's say the player bets 7 dollars. 假设玩家下注7美元。 What happens is div 'm' will flash with a number "-7" or "21" if, showing how much the player won or lost, and then immediately restore to being 200. If I put a "parseInt()" around the assignment of the variable "money", that just causes it to flash "NaN." 如果div'm'闪烁显示数字,则显示“ -7”或“ 21”,表示玩家赢了或输了多少,然后立即恢复为200。如果我将“ parseInt()”放在变量“ money”的赋值,只会使其闪烁“ NaN”。 Even if I ditch this concept and just put "var money = 200", it will flash "193" before returning to 200. 即使我放弃了这个概念,仅输入“ var money = 200”,在返回200之前它也会闪烁“ 193”。

How do I make this work? 我该如何工作? How do I have a currency variable that increases or decreases based on the results of a bet and can span multiple spins? 我如何有一个根据下注结果增加或减少并且可以跨越多次旋转的货币变量?

You are defining money in the head of the document, which gets executed before the the <div id="m"> is available. 您正在文档的开头定义money ,该money<div id="m">可用之前被执行。 You are also setting the value to the element rather than the text in the element. 您还将值设置为元素,而不是元素中的文本。 As a result money is undefined. 结果, money是不确定的。

You need to put that in an onload handler or right before the closing body tag. 您需要将其放在onload处理程序中或onload在body标记之前。

I don't know how you are doing document.regular_roulette , so this just uses a static value, but it might help: 我不知道你在做什么document.regular_roulette ,所以这只是使用一个静态值,但这可能会有所帮助:

 var money function spin() { var landing = Math.floor(Math.random() * 4) + 1; var nsvalue = parseInt(10); var betvalue = parseInt(10); if (landing === nsvalue) { document.getElementById('alert').innerHTML = "You win $" + betvalue * 3 money = money + (betvalue * 3); } else { document.getElementById('alert').innerHTML = "You lose $" + betvalue money = money - betvalue; } document.getElementById("m").innerText = money; } window.onload = function() { money = parseInt(document.getElementById("m").innerText); } 
 You currently have $ <div id="m">200</div> <div id="alert"></div> <button onclick="spin()">spin</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM