samedi 21 novembre 2015

DisplayObject must be child of caller

I am working on an inventory system for my game, as I am new to as3.0 I have everything inside my document class, the simple non code way of how this system works is that every object in the inventory starts out as a blank icon, when someone buys something the first object that is not a bought item in the inventory because the bought item. The inventory in this case is an array of objects. In the equip items screen when someone clicks an item, we remove the item from screen, and change it to a blank icon as well as doing whatever the item is supposed to do. The problem I am running into is after we click the object, remove the icon, and turn it into a blank icon, I cannot remove the array of objects when we switch screens. If i have the array of objects, I can remove the entire array from screen, but as soon as i modify it I can no longer remove it from the screen, I am not sure if the method i am using is completely wrong or if there is a small bug in the code. The code is below:

function addInventory():void
    {
        addedInv = true;
        for(var counterArray1:int = 0; counterArray1 < 8; counterArray1++){
            for(var counterArray2:int = 0; counterArray2 < 8; counterArray2++){
                this.addChild(objects[counterArray1*8 + counterArray2]);
                objects[counterArray1*8 + counterArray2].x = counterArray1*25.5 + 290.05;
                objects[counterArray1*8 + counterArray2].y = counterArray2*25.5 + 142;
            }
        }
    }

the above creates the inventory and places the objects on the screen.

function removeInventory():void{
        addedInv = false;
        for(var removeCounter:int = 0; removeCounter < 64; removeCounter++){
            this.removeChild(objects[removeCounter]);
        }
    }

The above removes the inventory from the screen.

function armor1item(event:MouseEvent):void{
        objects[objects.indexOf(event.currentTarget)] = new blank_Icon();
    }

That is what happens when someone clicks the object

function buy(event:MouseEvent):void{
        if(gold >= item.price){
        gold -= item.price
        if(iD == 1){
            a1c = new armor1_Icon();
            objects[objectsNum] = a1c;
            objects[objectsNum].addEventListener(MouseEvent.CLICK, armor1item);
        }
        if(iD == 2){
            s1c = new sword1_Icon();
            objects[objectsNum] = s1c;
            objects[objectsNum].addEventListener(MouseEvent.CLICK, sword1item);
        }
        objectsNum++;
        }
    }

When someone buys an item, we set the objects array to whatever they bought

function shopBack(event:MouseEvent):void
    {
        addPMenu();
        removeChild(backShop);
        if(addedInv){
            removeInventory();
            removeChild(equipItem);
        } else {
            removeChild(armor1);
            removeChild(sword1);
            removeChild(buyBtn);
        }
        gotoAndStop(12);
    }

The above is clicking the back button, we go to the other keyframe and remove stuff

What happens is when I click the object in the inventory, it is removed, then when I go back the other objects (everything that wasnt clicked) stays on screen, then I go back to the shop, everything that was removed is now a blank icon, the other items are the same. Then i go back again, and now everything is removed. I go back to the shop and all the items that are supposed to be there are.

Any help will be appreciated

Aucun commentaire:

Enregistrer un commentaire