I have an array full of currencies for my game (this.currency = {gold:0};
) and whenever I try to change the value it doesn’t always add 1+ the value but instead, it adds a new digit to the number. It only occurs when I use value += amount;
otherwise, it will just work fine.
What could be the cause of it and how can I fix it?
Full Script
var Main = pc.createScript('main');
// initialize code called once per entity
Main.prototype.initialize = function() {
/*--- prev session vals for temp---*/
var data = {
gold:sessionStorage.getItem('gold'),
multi:{
gold:sessionStorage.getItem('gmultip')
},
ship:sessionStorage.getItem('ship')
};
/*--- Vars ---*/
this.currency = {
gold: 0,
shards:0
};
this.multiplier = {
gold:1,
shards:1
};
this.automaticcrew = {
pirate:1,
captain:0,
officer:0,
getVals:function(){
return this.pirate+this.captain+this.officer;
}
};
this.ship = 0;
if(data.gold === undefined|| data.gold === null || data.ship === undefined|| data.ship === null){
// do nothing
}else{
this.ship = data.ship;
this.currency.gold = data.gold;
}
this.app.mouse.on(pc.EVENT_MOUSEDOWN,function(){
var amount = 1*this.multiplier.gold;
this.currency.gold++;
var ent = this.app.root.findByName('UpArrow').clone();
ent.enabled = true;
this.app.root.findByName('2D Screen').addChild(ent);
ent.setLocalPosition(pc.math.random(-screen.width/2,screen.width/2),pc.math.random(-screen.height/2,screen.height/2),0);
ent.findByName('Amount').element.text = amount;
var speed = 0.5;
setInterval(function(){
speed += 0.1/30;
ent.translateLocal(0,speed,0);
ent.element.opacity -= 0.01/3;
ent.findByName('Amount').element.opacity = ent.element.opacity;
if(ent.getLocalPosition().y > screen.height/2){
this.app.root.removeChild(ent);
}
}.bind(this),0);
},this);
};
// update code called every frame
Main.prototype.update = function(dt) {
var shipnode = this.app.root.findByName('Ships').children[this.ship];
this.app.root.findByName('Ships').children.forEach(function(node){
if(node.name != shipnode.name){
node.enabled = false;
}
});
shipnode.enabled = true;
this.app.root.findByName('Counter').element.text = this.currency.gold;
/*--- save values for temp---*/
sessionStorage.setItem('gold',this.currency.gold);
sessionStorage.setItem('ship',this.ship);
};