Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by ryananger

I've created an 'item' class that simply stores information for item stats on creation:

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?

1 decade ago by dominic

Where does n come from?
init: function (x,y,n,bool,settings) {
            this.itemId = n; // <- undefined?
            ...

1 decade ago by ryananger

Hey, dom, thanks for the reply. 'n' was coming from the ig.game.spawnEntity(arg1,arg2,n,etc) but I realized that spawnEntity doesn't take more than three arguments (I thought impact functions were functionally the same as regular js functions, with being able to add additional arguments at will).

That said, I ended up abandoning this approach anyway, and now I just have the stat generation code in the Entity init.
Page 1 of 1
« first « previous next › last »