javascript同时增加1

时间:2015-01-30 15:13:47

标签: javascript increment

我希望你可以帮助我解决我的这个小问题,我现在正在尝试开发一个增量游戏,用于娱乐和学习目的,但我无法理解如何让它同时增加1, 就像我有100件/秒。我希望它每秒加1到100,

所以相反 0 / *第二次时间流逝* / 100

我想要它 0-1-2-3-4-5-6等 当然这是我的代码。

    // the current amount of snus
this.snusAmount = 0.0;
//the current amount of snus per second
this.snuspSec = 0.0;
this.increaseBy = {knox: 0.1, offroad: 0.5, ld: 10, ettan: 19, goteborg: 57, general: 126, nick: 500, grov: 1337};
this.getSnus = getSnus;


function getSnus(){
var number = Math.abs(this.snusAmount);
var fixedNumb = number.toFixed(1);
return fixedNumb;
}

function addSnus(){
    this.snusAmount += this.snuspSec;
}

function getSnusPSec(){
    return parseFloat(this.snuspSec.toFixed(1));
}

如果您需要更多信息,我会尝试尽可能地添加它。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

编辑:我创造了一个迷你赚钱游戏。它还没有处理负面因素(就像你可以购买并有负面资金)。它也没有处理小数。但是,这应该让你在问题的目的和评论的修改范围内开始你想要的东西。

我只有2个元素,但是你可以创建一个数组,然后循环遍历数组以供增加收入者使用。

$(document).ready(function() {
    var totalMoney = 0;
    var storeCount = 0;
    var hotelCount = 0;
    $('.addMoney').on('click', function(e) {
        e.preventDefault();
        totalMoney = checkTotalMoney();
        totalMoney++;
        $('.money').html(totalMoney);
    });
    $('.addStore').on('click', function(e) {
        e.preventDefault();
        var currentTotal = checkTotalMoney();
        $('.money').html(currentTotal - 10); // subtract $10
        storeCount++;
    });
    $('.addHotel').on('click', function(e) {
        e.preventDefault();
        var currentTotal = checkTotalMoney();
        $('.money').html(currentTotal - 50); // subtract $10
        hotelCount++;
    });
    
    
    setInterval(function(){ 
        var currentTotal = checkTotalMoney();
        var totalStoreIncome = 0;
        var totalHotelIncome = 0;
        if (storeCount > 0) {
            totalStoreIncome = 1 * storeCount; // rate of 1/second
        }
        if (hotelCount > 0) {
            totalHotelIncome = 6 * hotelCount; // rate of 6/second
        }
        currentTotal = currentTotal + totalStoreIncome + totalHotelIncome;
        $('.money').html(currentTotal);
        
        if (currentTotal >= 50) //show different buying options
        {
            $('.hotel').show();
            $('.store').show();
        }
        else if (currentTotal >= 10) {
            $('.hotel').hide();
            $('.store').show();
        }
        else {
            $('.hotel').hide();
            $('.store').hide();
        }
        
        updateCounts(storeCount, totalStoreIncome, hotelCount, totalHotelIncome);
    }, 1000);
});

function checkTotalMoney() {
    return parseInt($('.money').html());
}
function updateCounts(storeCount, totalStoreIncome, hotelCount, totalHotelIncome) {
    $('.storeIncome .count').html(storeCount);
    $('.storeIncome .profitRate').html(totalStoreIncome);
    $('.hotelIncome .count').html(hotelCount);
    $('.hotelIncome .profitRate').html(totalHotelIncome);
    $('.totalProfit').html(parseInt(totalStoreIncome) + parseInt(totalHotelIncome));
}
.income div {
    border: 1px solid black;
    display: inline-block;
    padding: 10px;
    margin: 10px;
}
.items div {
    background-color:lightgray;
    border: 1px solid black;
    display: inline-block;
    padding: 10px;
    margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You have: $<span class="money">0</span><br/><br/>
You are earning income from the following:
<div class="income">
    <div class="storeIncome">
        Stores owned: <span class="count">0</span><br/>
        Rate of profit of each store: $1/second<br/>
        Total Stores Profit: $<span class="profitRate">0</span> / second
    </div>
    <div class="hotelIncome">
        Hotels owned: <span class="count">0</span><br/>
        Rate of profit of each store: $6/second<br/>
        Total Hotels Profit: $<span class="profitRate">0</span> / second
    </div><br/><br/>
    Total Profit from Stores and Hotels: $<span class="totalProfit">0</span> / second<br/>
</div>
<br/><br/>
<a href="#" class="addMoney">Earn $1</a>
<br/><br/>
<div class="items">
    <div class="store" style="display:none;">
        Buy a STORE for $10?<br/>
        Rate of income: $1/second<br/><br/>
        <a href="#" class="addStore">Buy STORE</a>
    </div>
    <div class="hotel" style="display:none;">
        Buy a STORE for $50?<br/>
        Rate of income: $6/second<br/><br/>
        <a href="#" class="addHotel">Buy Hotel</a>
    </div>
</div>

相关问题