嗯,我的代码一直工作正常,我只是想做一个简单的游戏,直到我添加了这个(由于我想了解如何将信息保存到用户本地存储) :localStorage不断添加'0'。 (jScript&HTML5)
if(localStorage.getItem('money'))
{
var Money = localStorage.getItem('money');
document.getElementById('test').innerHTML="EXISTS";
} else {
localStorage.setItem('money', 0);
var Money = localStorage.getItem('money');
document.getElementById('test').innerHTML="DOES NOT EXIST";
}
我完整的代码看起来是这样的:
Generated something: 0$ (money per click: 1$)
Upgrade 1 upgrades: 0 (production rate: 0)
Generate something.
Upgrades:
Generator upgrades:
Buy upgrade 1. ($30)
Self-generated upgrades:
Buy Self-Generated Upgrade 1 ($500)
if(localStorage.getItem('money'))
{
var Money = localStorage.getItem('money');
document.getElementById('test').innerHTML="EXISTS";
} else {
localStorage.setItem('money', 0);
var Money = localStorage.getItem('money');
document.getElementById('test').innerHTML="DOES NOT EXIST";
}
var U1Amount = 0;
var cMoney = 1;
function mButton()
{
Money += cMoney;
document.getElementById('money').innerHTML=Money + "$";
}
function buyU1()
{
if(Money < 30)
{
document.getElementById('updates').innerHTML="You do not have enough money.";
resetUpdates();
} else {
Money -= 30;
U1Amount += 1;
document.getElementById('u1Us').innerHTML=U1Amount;
document.getElementById('money').innerHTML=Money + "$";
document.getElementById('updates').innerHTML="You have successfully bought Upgrade 1";
var calcU1Amount = U1Amount * 5;
document.getElementById('u1Pr').innerHTML=calcU1Amount;
resetUpdates();
}
}
var interval = setInterval(gMoneyU1, 1000);
function gMoneyU1()
{
var calc = 5 * U1Amount;
Money += calc;
document.getElementById('money').innerHTML=Money + "$";
}
function buySG1()
{
if(Money < 500)
{
document.getElementById('updates').innerHTML="You do not have enough money.";
resetUpdates();
} else {
Money -= 500;
cMoney += 1;
document.getElementById('MPC').innerHTML=cMoney + "$";
document.getElementById('money').innerHTML=Money;
}
}
function resetUpdates()
{
setTimeout(function(){document.getElementById('updates').innerHTML="";}, 10000);
}
我要去localStorage的增加,我要保存所有的信息,但我在使用的第一个问题,所以揭掉。 什么我和我保存最者,信息输出的代码是:http://puu.sh/6iONl.png(和它的所有永远不断)
它不断增加对某些原因“0”,我想不通为什么。我真的很感激帮助。 在此先感谢。
对不起,如果我的代码很混乱,我还在学习,所以为什么要求帮助。
2014-01-12
prk