在数组中使用实例名称,#1010错误,as3

时间:2010-06-22 13:58:01

标签: flash actionscript-3 actionscript

我想在数组中使用实例,但是会出错。如何在数组中使用实例?感谢。

错误1010'术语未定义且没有属性

//I'm trying to make two array objects disappear
var pink:Array = ["boxInstance1","boxInstance2"];
/*
THIS WORKS
boxInstance1.visible = false;
boxInstance2.visible = false;
*/
//THIS DON'T 'or with one instance in the array it works'
this[pink].visible = false;

3 个答案:

答案 0 :(得分:1)

在数组中有一个实例,flash将数组转换为字符串,并获得boxInstance1作为值;多个值数组转换为boxInstance1,boxInstance2(可能),因此错误。使用[]

使用正确索引处的值
this[pink[0]].visible = false;
//equivalent to
boxInstance1.visible = false; 

this[pink[1]].visible = false;
//equivalent to
boxInstance2.visible = false; 

for(var i:Number = 0; i < pink.length; i++)
  this[pink[i]].visible = false;

答案 1 :(得分:0)

您需要使用getChildByName()

var mc:MovieClip = new MovieClip();
mc = getChildByName(pink[0]);
mc.visible = false;

mc = getChildByName(pink[1]);
mc.visible = false;

如果要对所有实例执行某些操作,请使用for循环:

var mc:MovieClip;
for(var i:int = 0; i < pink.length; i++)
{
    mc = MovieClip(getChildByName(pink[i]));
    mc.visible = false;
}

答案 2 :(得分:0)

可替换地:

var pink:Array = [this.boxInstance1, this.boxInstance2];
for each(var box:Sprite in pink)
    box.visible = false;