mardi 13 décembre 2016

AS3 object passed as e.target does not match identical object passed through its array index

im designing an inventory system. right now, i need to test whether or not an item is in the inventory in order for the stage to know whether to instantiate that item in the particular level or not.

i add the items to the levels in groups, so this code is located within an array loop which "unloads" the "pack" of items corresponding to each level.

if (inv.indexOf(group[i]) == -1) {
    //add item + item functionality
}

this method works when i add the item to the inventory such as this:

inv.push(group[i]);
if (inv.indexOf(group[i]) == -1) {
    //add item + item functionality
}

but that doesn't work, because why would i add an item to the inventory without the user collecting it first? so the code is actually structured as so:

            if (inv.indexOf(group[i]) == -1) {

                //if item is not in inventory, add to stage
                addChild(group[i]);

                //when a user clicks this (any) item,
                group[i].addEventListener(MouseEvent.CLICK, function itemFunctionality(e:MouseEvent){

                    //target item clicked
                    var item = e.target;
                    //add the item to the inventory
                    inv.push(item);
                    //sidenote: if i were to check inv.indexOf(item) here, i 
                    //would get a positive index. unfortunately,
                    //i cant check whether the item is in the inventory
                    //after its already been added to the level...

                    item.removeEventListener(MouseEvent.CLICK, itemFunctionality);
            });
            }

the problem is when you leave and come back to the level, the items you already collected re-instantiate.

if you collect an item again, the inventory adds a copy of the item you already collected.

the inv.indexOf(group[i]) checker doesn't understand that when the array loop reaches the corresponding, item group[i] == the object added to the inventory through inv.push(item) or in other words inv.push(e.target) (which, of course, i couldnt write directly into the code)...

when i trace whats inside of static array inv, what group[i] is within the array loop, or what e.target is, they all output the same type of item, "[object itemName]", signifying that the indexOf check SHOULD match up.

what the heck!!!

Aucun commentaire:

Enregistrer un commentaire