单击后JQuery更改变量/成本

时间:2015-08-25 13:50:14

标签: javascript jquery html

嘿我试图创造一个增量游戏而且陷入困境。 你按一个按钮就可以获得一种名为" Evos"的货币。有了这些" Evos"你可以买一个故事讲述者,他将开始生成" Evos"独自一人。

这是代码:



$(document).ready(function() {
"use strict";
var evoAmount = 0;

var storytellerAmount = 0;

var evoIncrement = 1;

var storyteller = {
    Amount: 0,
    Cost: 10,
    Increment: 1
};

var tick = 1000;




var runstoryteller = setInterval(function () {
    evoAmount = evoAmount + (storyteller.Increment * storyteller.Amount);
    updateValues();
}, tick);

function updateValues(){
     $('#evoAmount').html(evoAmount);
     $('#storytellerAmount').html(storyteller.Amount);
}


/* Buy storytellers */
$('#storytellerBuy').click(function () {
    if (evoAmount >= storyteller.Cost) {
        evoAmount = evoAmount - storyteller.Cost;
        storyteller.Amount++;
		storyteller.Cost = (storyteller.Cost / 100) * 20;
        updateValues();
    }
});





$('#click').click(function(){
	evoAmount = evoAmount + evoIncrement;
	document.getElementById("evoAmount").innerHTML = evoAmount;
		
});












});

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://bootswatch.com/slate/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title>Evo-Clicker v1.0.0</title>
</head>

<body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/click.js"></script>

<div class="main"> 
<button id="click" class="btn btn-default">Click</button>
<span id="evoAmount">0</span> EVOs <br><br>
<button id="storytellerBuy" class="btn btn-danger">Buy Storyteller</button><span id ="storytellerAmount">0</span> Storytellers

</div>


</body>
</html>
&#13;
&#13;
&#13;

基本上我可以购买10个Evos的第一个故事讲述者。但是我希望每个买入的讲故事者的成本增加20%。但是,如果我改变代码,它或者进入负面&#34; Evos&#34;或者在这个例子中,您只需支付一次,然后免费购买故事讲述者。

/* Buy storytellers */
$('#storytellerBuy').click(function () {
    if (evoAmount >= storyteller.Cost) {
        evoAmount = evoAmount - storyteller.Cost;
        storyteller.Amount++;
        updateValues();
    }
});

这样可行,但保持不变。 (我想每次增加20%)

1 个答案:

答案 0 :(得分:0)

您应该替换成本计算

这是正确的 storyteller.Cost =(storyteller.Cost * 1.2);