Cookie点击器游戏多变量商店

时间:2018-05-27 16:24:50

标签: javascript html

我刚刚开始编写代码,在几天的学习之后,我决定将代码用于良好的用途。我开始使用像游戏一样的cookie点击器,但有所不同。我有多种资源(石油,黄金)。我取得了很大的进步,但后来我开始逛街了。但不是正常的商店,有一些项目可以提高你的收益,但有些东西可用于战争(我还没有,所以我只想让它成为现在的反击),事情就是它使用多种资源(黄金,石油)我采取了生产促进这些材料的代码,并改变它,以便它消耗两种资源。但现在它低于0这是不好的。我花了1.5小时在这6行代码上像白痴一样,我希望你能帮助我。 所以这是我认为工作正常的按钮

<button onclick="automobily()"><p id="cars1"></p></button>

这应该是按钮上写的内容(至少我说它应该是这样)

document.getElementById("cars1").innerHTML = "Car (0) 100 oil and 50 gold";

这就是问题......

function automobily() {
if (oil >= 100 + (cars1))  (gold >= 50 + (cars1)); {
gold -= 50 ;oil -= 100 ;cars1 += 1
carcost = 50    
document.getElementById("oil").innerHTML = oil;
document.getElementById("gold").innerHTML = gold;
document.getElementById("cars1").innerHTML = "cars (" + cars1 + ")" + carcost + " 100 oil and 50 gold";

}
}

应该可以正常工作......(至少我希望如此) 所以主要的问题是它变得消极。还请注意,我是一个初学者,我不理解所有的编码术语,所以如果你能解释它有点深刻。提前致谢。 -Holsp

PS:这是运行它的代码。

    <big><b><h1>Gold</h1></b></big>

<img src="mince.png" alt="mince" style="width:180px;height:180px;border:0;" onclick="clickme()" />

<h2><bold><big>You have <element id="gold"></element> grams of gold</big></bold></h2>

<p id="gold"></p><br><br>
<script>
gold = 0;
cps = 0;
autoGs = 0;
employGs = 0;
clicker = 1;
adminGs = 0;
function clickme() {
gold += clicker;
document.getElementById("gold").innerHTML = gold;
}
</script>

<br></br>
<img src="barel.png" alt="Barel" style="width:200px;height:180px;border:0;" onclick="clickme1()" />

<h2><bold><big>You have <element id="oil"></element> barrels of oil</big></bold></h2>

<script>
oil = 0;
cps1 = 0;
bakers1 = 0;
godlybakers1 = 0;
clicker1 = 1;
gloves1 = 0;
function clickme1() {
oil += clicker1;
document.getElementById("oil").innerHTML = oil;
}
</script>

<button onclick="automobily()"><p id="cars1"></p></button>

<script>
document.getElementById("cars1").innerHTML = "Car (0) 100 oil and 50 gold";

function automobily() {
    if (oil >= 100 + (cars1))  (gold >= 50 + (cars1)); {
    gold -= 50 ;oil -= 100 ;cars1 += 1
    carcost = 50    
    document.getElementById("oil").innerHTML = oil;
    document.getElementById("gold").innerHTML = gold;
    document.getElementById("cars1").innerHTML = "cars (" + cars1 + ")" + carcost + " 100 oil and 50 gold";

    }
    }
</script>   

1 个答案:

答案 0 :(得分:0)

我认为改变你的条件可以解决这个问题:

if ( (oil >= 100 + (cars1)) && (gold >= 50 + (cars1)) )

完整代码

&#13;
&#13;
oil = 0;
cps1 = 0;
bakers1 = 0;
godlybakers1 = 0;
clicker1 = 1;
gloves1 = 0;

function clickme1() {
  oil += clicker1;
  document.getElementById("oil").innerHTML = oil;
}

gold = 0;
cps = 0;
autoGs = 0;
employGs = 0;
clicker = 1;
adminGs = 0;

function clickme() {
  gold += clicker;
  document.getElementById("gold").innerHTML = gold;
}

document.getElementById("cars1").innerHTML = "Car (0) 100 oil and 50 gold";

function automobily() {
  if (oil >= 100 + cars1 && gold >= 50 + cars1) {
    gold -= 50;
    oil -= 100;
    cars1 += 1;
    carcost = 50;
    document.getElementById("oil").innerHTML = oil;
    document.getElementById("gold").innerHTML = gold;
    document.getElementById("cars1").innerHTML =
      "cars (" + cars1 + ")" + carcost + " 100 oil and 50 gold";
  }
}
&#13;
<big><b><h1>Gold</h1></b></big>

<img src="mince.png" alt="mince" style="width:180px;height:180px;border:0;" onclick="clickme()" />

<h2>
  <bold><big>You have <element id="gold"></element> grams of gold</big></bold>
</h2>

<p id="gold"></p><br><br>

<br></br>
<img src="barel.png" alt="Barel" style="width:200px;height:180px;border:0;" onclick="clickme1()" />

<h2>
  <bold><big>You have <element id="oil"></element> barrels of oil</big></bold>
</h2>

<button onclick="automobily()"><p id="cars1"></p></button>
&#13;
&#13;
&#13;

此外:

  • 使用varletconst个关键字声明变量。

  • 将您的Js脚本放在html文件之外