开发者

use instance names in array, #1010 error, as3

I want to use instances in an array, but get an error. How do I use instances in an array? Thanks.

Error 1010 'A term is undefined and has no properties'

//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].vis开发者_如何学Cible = false;


With one instance in array, flash converts the array into string and you get boxInstance1 as the value; with multiple values array gets converted as boxInstance1,boxInstance2 (possibly) and hence the error. Use the value at correct index using []

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;


Alternately:

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


You need to use getChildByName():

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

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

If you want to do something to all instances, use a for loop:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜