我可以轻松地在python http://prntscr.com/ns14y9中使用它,但是即时通讯最难在javascript上复制它。我希望我的马力和法力值仅在第一次重生购买后增加。
我尝试做一个if语句,但它似乎根本不起作用或im格式错误。
这是我的Javascript:
var rebirths = 0;
var HitPoints = 0;
var ManaPoints = 0;
function buyRebirth() {
var rebirthCost = Math.floor(10 * Math.pow(1.1, rebirths));
if (cookies >= rebirthCost) {
cookies = cookies - rebirthCost
HitPoints = HitPoints + 10;
ManaPoints = ManaPoints + 10;
rebirths = rebirths + 1;
document.getElementById("rebirths").innerHTML = rebirths;
document.getElementById('cookies').innerHTML = cookies;
document.getElementById("HitPoints").innerHTML = HitPoints;
document.getElementById("ManaPoints").innerHTML = ManaPoints;
document.getElementById("rebirthCost").innerHTML = rebirthCost;
这是我的html:
</style>
<span id="cookies">0</span>
<br />
<span id="rebirths">0</span>
<br />
<button onclick="cookieClick(1)">Click Me!</button>
<br />
Cost: 50 <button id="BigClickBtn"
onclick="cookieClick(50)">BigClick</button>
<br />
Cost: <span id="cursorCost">10</span> <button
id="cursorCostBtn" onclick="buyCursor()">Buy Cursor</button>
<br />
Cost: <span id="catCost">50</span> <button
onclick="buyCat()" id="catCostBtn">Buy Cat</button>
<br />
Cost: <span id="dogCost">100</span> <button
onclick="buyDog()" id="dogCostBtn">Buy Dog</button>
<br />
Cost: <span id="humanCost">200</span> <button
onclick="buyHuman()" id="humanCostBtn">Buy Human</button>
<br />
Cost: <span id="rebirthCost">10</span> <button
onclick="buyRebirth()" id="rebirthCostBtn">Rebirth</button>
<br />
Hp:<span id="HitPoints">0</span> Mp:<span
id="ManaPoints">0</span>
<script type="text/javascript" src="main.js"></script>
</body>
我预计在重生> 1之后,Hp和Mp将停止增加。但是每次我购买重生时,Hp和Mp的实际输出都会增加10。
最佳答案
不幸的是,您没有发布buyRebirth()
函数的完整定义,但是显然您没有在将HitPoints和ManaPoints增加10之前检查是否有多个重生。
HitPoints = HitPoints + 10;
ManaPoints = ManaPoints + 10;
将其包装在简单的if条件中:
if (rebirths == 0) {
HitPoints = HitPoints + 10;
ManaPoints = ManaPoints + 10;
}
rebirths = rebirths + 1;
下次您输入buyRebirth时,重生的值将大于0,因此条件rebirths == 0的计算结果为false。