actionscript-3 - 数组中的 AS3 变量不会更新,即使直接给出 "command"

标签 actionscript-3 variables air flash-cc

希望你一切都好。 我有一个我不明白的问题。我的变量似乎更新了,但它们没有(我在下面的代码中在上下文中进一步解释了)。 [我正在尝试创建一家商店,让我的用户可以为我的游戏购买 Boss]。

我真的希望你能帮助我:)

这是代码。请随时向我询问更多详情(Boss 总数约为 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.
    }
}

最佳答案

当你这样做时:

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

您从 sp 数组中取出变量并将它们放入 bossVar 数组中。在这种情况下,int 变量被复制。

这意味着更改 sp[59]bossVar[0] 不会更改另一个。

示例代码:

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

例如,您可以通过存储对象来规避这一点。

关于actionscript-3 - 数组中的 AS3 变量不会更新,即使直接给出 "command",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30174583/

相关文章:

android - 是什么导致此错误 : "SDK component at air/android/device/Runtime.apk is out of date"

actionscript-3 - Adobe AIR 应用程序音频

apache-flex - As3 Graph API 注销

actionscript-3 - ActionScript 3 标签粗体

php - 将输入传递给 C 程序并使用 php 获取输出

您可以使用 Adob​​e 的 Alchemy 来执行批处理文件吗?

c++ - 变量引用和名称之间的区别

php - 如何在 PHP 中编写一个简单的对象

apache-flex - ActionScript :未定义的公共(public)变量?

flash - AS3 中的 URL 编码变量?