vendredi 20 novembre 2015

adding object from array to game as3

Before I proceed, Please understand that I am new to as3.0 The problem is, I want to have an 'inventory' in my game. I have done this by adding an array and then every time i go to the inventory keyframe I add the objects onto the screen. I first initialize the array with 'blank icons' done like this: note that blank_Icon is an object in library with linkage: blank_Icon

var blankIcon:blank_Icon;
for (var objArray1:int = 0; objArray1 < 64; objArray1++)
                blankIcon = new blank_Icon();
                objects[objArray1] = blankIcon;

I add the objects to the stage using this code:

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;

I use the same idea to remove the objects using remove child(objects[i])in a for loop going from 0 - 63, this works perfectly. Now the problem is when I want to buy an item, I want to change the object in the objects array to the icon of the item they bought. I have done this like so:

function buy(event:MouseEvent):void{
        if(gold >= item.price){
        gold -= item.price
        armor += item.armor;
        speed += item.speed;
        attack += item.attack;
        hp += item.hp;
        manaM += item.manaM;
        manaR += item.manaR;
        if(item.Id == "a1c"){
            a1c = new armor1_Icon();
            objects[objectsNum] = a1c;
        if(item.Id == "s1c"){
            s1c = new sword1_Icon();
            objects[objectsNum] = s1c;
        goldText.text = gold.toString();

Majority of that is for the use of the game, I have an object 'item'; I wanted a struct like they have in c++, and after research I found that you declare them as objects like such. Now when the user clicks on 1 object I set the values to whatever, and item.Id to either a1c or s1c. However, when the user buys the item the array objects is not manipulated at all.

Aucun commentaire:

Enregistrer un commentaire