数组中的 AS3 变量不更新,即使直接给定 "command"
AS3 variables from array not updating, even when given direct "command"
希望你一切都好。
我有一个我不明白的问题。我的变量似乎更新了,但它们没有更新(我在下面的代码中进一步解释了上下文)。
[我正在尝试创建一个商店,我的用户可以在其中为我的游戏购买 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]);
例如,您可以通过存储对象来避免这种情况。
希望你一切都好。 我有一个我不明白的问题。我的变量似乎更新了,但它们没有更新(我在下面的代码中进一步解释了上下文)。 [我正在尝试创建一个商店,我的用户可以在其中为我的游戏购买 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]);
例如,您可以通过存储对象来避免这种情况。