Search code examples
actionscript-3variablesairflash-cc

AS3 variables from array not updating, even when given direct "command"


I hope you're well. I've a problem that I do not understand. My variables seem to update but they don't (i've explained further in context in the code below). [I'm trying to create a store where my users can buy bosses for my game].

I really hope you can help me :)

Here is the code. Do not hesitate to ask me for further details (number total of bosses will be around 36).

var bossBuyBtnArray:Array=[campagnesBuy.boss1, campagnesBuy.boss2]; 
//array with the MovieClips used as button to buy
var pbArray:Array=[campagnesBuy.pb1, campagnesBuy.pb2]; //array with MovieClips that tells if boss is owned or not
var bossVar:Array=[sp[59], sp[60]]; //array with int variables needed to save the state of inventory

for each(var storeBtn: MovieClip in bossBuyBtnArray) 
storeBtn.addEventListener(MouseEvent.CLICK, fnBossStoreBtn); //when user click on the store buttons, 
function fnBossStoreBtn(e: Event): void {
    var listElementBoss: DisplayObject = e.target as DisplayObject; //listeElementBoss = Selected Boss.
    var iBoss: int = bossBuyBtnArray.indexOf(listElementBoss); //get the index of the selected boss
    if (iBoss < 0) { //check if selected boss is in the array
        listElementBoss = e.currentTarget as DisplayObject;
        if (listElementBoss) iBoss = bossBuyBtnArray.indexOf(listElementBoss);
    }
    if (iBoss < 0) return;
    if(pbArray[iBoss].currentFrame == 1){ //check if boss not already owned.
        if (sp[58]>999){ //check if user has enough gold.

            /*The Part that Doesn't Work : */
            bossVar[iBoss] = 1; //modify the variable (sp[59] or sp[60])
            //normally it would update the variable, let's say sp[59] if boss 1 is selected. 
            //The interface is modified by fnAlreadyOwned (see below) so I guess something is updated,
            //But when I trace(sp[59]), it says 0, and the writeObject() function saves 0. Meaning that
            //when I reload the game, the gold is gone, but boss is locked again.
            /*The rest works*/

            sp[58] = sp[58] -1000; //substract cost of the boss from the total gold.
            gameMode.pirateTxt.text = sp[58]; //update gold "inventory"
            writeObject(); //save the state of gold and boss ownership
            fnAlreadyOwned(null);// updates the interface
        }else{
            trace("not enough gold"); //if not enough gold
        }
    }else{
        trace("already owned"); //if boss already owned.
    }
}

Solution

  • When you do this:

    var bossVar:Array=[sp[59], sp[60]]; //array with int variables needed to save the state of inventory
    

    you take the variables from the sp array and put them in bossVar array. int variables are copied in this case.

    That means changing either sp[59] or bossVar[0] won't change the other.

    example code:

    var a:Array = [1, 2];
    var b:Array = [a[0], 5];
    a[0] = 42;
    trace("a[0] =" + a[0]);
    trace("b[0] =" + b[0]);
    

    You could circumvent that by storing objects for example.