为什么只有四个按钮中的第一个功能起作用?

时间:2019-05-22 01:13:32

标签: javascript html css

buyCursor函数有效,其余函数无效。我希望buyCat花费50,然后再花费100,然后再花费200。我还想设置一个花费1000的按钮,该按钮会将所有内容重置为零,并将重生计数器加1。

我尝试复制/粘贴第一个功能,然后将其编辑为其他购买按钮。我不确定自己是否做对了100%。

这是我的代码:

var cookies = 0;
function cookieClick(number) {
    cookies = cookies + number;
    document.getElementById("cookies").innerHTML = cookies;
}

var cursors = 0;

function buyCursor() {
    var cursorCost = Math.floor(10 * Math.pow(1.1,cursors));     //works out the cost of this cursor
    if (cookies >= cursorCost) {                                   //checks that the player can afford the cursor
        cursors = cursors + 1;                                    //increases number of cursors
    	cookies = cookies - cursorCost;                          //removes the cookies spent
        document.getElementById('cursors').innerHTML = cursors;  //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
    }
    var nextCost = Math.floor(10 * Math.pow(1.1,cursors));       //works out the cost of the next cursor
    document.getElementById('cursorCost').innerHTML = nextCost;  //updates the cursor cost for the user
}

var cats = 0;

function buyCat() {
    var catCost = Math.floor(50 * Math.pow(1.1,cats));           //works out the cost of this cursor
    if (cookies >= catCost) {                                      //checks that the player can afford the cursor
        cats = cats + 2;                                         //increases number of cursors
    	cookies = cookies - catCost;                             //removes the cookies spent
        document.getElementById('cats').innerHTML = cats;        //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
    }
    var nextCost = Math.floor(50 * Math.pow(1.1,cats));          //works out the cost of the next cursor
    document.getElementById('catCost').innerHTML = nextCost;     //updates the cursor cost for the user
}

var dogs = 0;
function buyDog(){
    var dogCost = Math.floor(100 * Math.pow(1.1,dogs));          //works out the cost of this cursor
    if (cookies >= dogCost) {                                      //checks that the player can afford the cursor
        dogs = dogs + 5;                                         //increases number of cursors
        cookies = cookies - cursorCost;                          //removes the cookies spent
        document.getElementById('dogs').innerHTML = dogs;        //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
    }
    var nextCost = Math.floor(100 * Math.pow(1.1,dogs));         //works out the cost of the next cursor
    document.getElementById('dogCost').innerHTML = nextCost;     //updates the cursor cost for the user
}
 
var humans = 0;
function buyHuman() {
    var humanCost = Math.floor(200 * Math.pow(1.1,humans));      //works out the cost of this cursor
    if (cookies >= humanCost) {                                    //checks that the player can afford the cursor
        humans = humans + 8;                                     //increases number of cursors
    	cookies = cookies - cursorCost;                          //removes the cookies spent
        document.getElementById('humans').innerHTML = humans;    //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
    }
    var nextCost = Math.floor(200 * Math.pow(1.1,humans));       //works out the cost of the next cursor
    document.getElementById('humanCost').innerHTML = nextCost;   //updatesthe cursor cost for the user
}

var rebirths = 0;
function buyRebirth() {
    var rebirthCost = Math.floor(1000*Math.pow(1.5,rebirths));
    if (cookies >= rebirthCost) {
        cookies = cookies - rebirthCost;
        document.getElementById('rebirths').innerHTML = rebirths; //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;   //updates the number of cookies for the user
    }
    var nextCost = Math.floor(200 * Math.pow(1.5,rebirths));      //works out the cost of the next cursor
    document.getElementById('rebirthCost').innerHTML = nextCost;  //updates the cursor cost for the user
}

window.setInterval(function() {
	cookieClick(cursors);
}, 1000);
<span id="cookies">0</span><br/>

<button onclick="cookieClick(1)">Click Me!</button><br/>

Cost: 10 mp <button onclick="cookieClick(1)">BigClick</button><br/>

Cost: <span id="cursorCost">10</span>
<button onclick="buyCursor()">Buy Cursor</button><br />

Cost: <span id="catCost">50</span>
<button onclick="buyCat()">Buy Cat</button><br />

Cost: <span id="dogCost">100</span>
<button onclick="buyDog()">Buy Dog</button><br />

Cost: <span id="humanCost">200</span>
<button onclick="buyHuman()">Buy Human</button><br />

Cost: <span id="rebirthCost">1000</span>
<button onclick="buyRebirth()">Rebirth</button><br />

我的第一个购买功能有效,其余功能无效。我希望每个人都花更多的钱去购买,然后再增加总数。我也确实想将所有“ cookies”更改为“ counter”,但恐怕很难使其再次起作用。我还想知道如何在cookies>=itemCost之前禁用按钮。

0 个答案:

没有答案