1 decade ago by ryananger
I've created an 'item' class that simply stores information for item stats on creation:
Then, whenever an item entity is spawned, using a random value called in the entity's init function, it pulls information from the above to create a set of stats specific to the spawned entity. I then store this information in an array in main.js.
What I'm expecting to happen with this code, is that for each new instance of an item, the random 'r1' variable will change the stats of that particular item. When it is stored in the ig.game.itemArray, it should differ from the previous entities in the array.
What is ACTUALLY happening is this: Each time I spawn a new item, it gets random stats based on the 'r1' variable (as expected), BUT each previously spawned item in the array changes stats to match those of the new one.
I absolutely can't figure out for the life of me why this happening. Anyone have any ideas?
itemlist = ig.Class.extend({ itemArray: [], init: function() { for (i = 10; i < 10000; i++) { this.itemArray[i] = { name: 0, basetype: 0, ntype: 0, type: 0, tier: 0, stats: {armor: 0, str: 0, agi: 0, will: 0, speed: 0, dmg: 0}, x: 0, y: 0 } } }, itemCreation: function(i,r1) { var n = i/10; var q = this.itemArray[i]; q.y = Math.floor(n); q.x = Math.round((n - q.y)*10); var statpool = q.tier*10 switch (q.ntype) { case 2: statpool += q.tier*0; q.stats.armor = 3*q.tier; statpool -= q.stats.armor; // 'r1' is a random value generated when item entity is spawned q.stats.str = r1; statpool -= q.stats.str; q.stats.armor += statpool; break; } } });
Then, whenever an item entity is spawned, using a random value called in the entity's init function, it pulls information from the above to create a set of stats specific to the spawned entity. I then store this information in an array in main.js.
init: function (x,y,n,bool,settings) { this.itemId = n; // ig.game.itemlist is the class above that generates item information var q = ig.game.itemlist.itemArray[n]; var statpool = q.tier*10; var r1 = Math.floor(Math.random()*(statpool-(5*q.tier))); ig.game.itemlist.itemCreation(n,r1); this.info = { name: q.name, basetype: q.basetype, ntype: q.ntype, type: q.type, tier: q.tier, stats: q.stats } ig.game.itemArray[ig.game.itemArray.length] = this.info;
What I'm expecting to happen with this code, is that for each new instance of an item, the random 'r1' variable will change the stats of that particular item. When it is stored in the ig.game.itemArray, it should differ from the previous entities in the array.
What is ACTUALLY happening is this: Each time I spawn a new item, it gets random stats based on the 'r1' variable (as expected), BUT each previously spawned item in the array changes stats to match those of the new one.
I absolutely can't figure out for the life of me why this happening. Anyone have any ideas?