Как получить доступ к Movieclips индивидуально в цикле as3
Скажем так mySaveNewT.data.myNText = 20
и за цикл, 20 MovieClips (tbox
) населены на сцене. Когда tbox
Экземпляр нажимается, я хочу изменить его видимость false
,
Как мне сослаться на отдельный MovieClip, на который нажимают, без необходимости устанавливать видимость каждого MovieClip на false? (т.е. если
MC[2]
а такжеMC[10]
получить щелчок, а остальные нет)Как мне вставить это в массив?
Вот мой цикл:
for (var i: Number = 0; i < mySaveNewT.data.myNText; ++i) {
newText = new tbox();
newText.x = -220;
newText.y = -513 + i * 69 + 0 * 3.8;
VWD.addChild(newText);
}
1 ответ
Решение
Чтобы вставить массив, добавить прослушиватель кликов и изменить видимость, см. Комментарии к коду:
//you need to define an array to store the clips in
var clickedBoxes:Array = []; //this makes a new empty array, same as doing: = new Array();
for (var i: Number = 0; i < mySaveNewT.data.myNText; ++i) {
newText = new tbox();
newText.x = -220;
newText.y = -513 + i * 69 + 0 * 3.8;
VWD.addChild(newText);
newText.addEventListener(MouseEvent.CLICK, clipClickHandler,false,0,true); //now you add a click listener to this clip
}
function clipClickHandler(e:MouseEvent):void {
//e.currentTarget will be a reference to the item that was clicked
MovieClip(e.currentTarget).visible= false; //we wrap e.currentTarget in MovieClip so the compiler knows it has a visible property (casting)
clickedBoxes.push(e.currentTarget);
}
Чтобы перебрать ваш массив позже:
for(var index:int=0;index<clickedBoxes.length;index++){
clickedBoxes[index].visible = true; //you may have to cast to avoid a compiler error MovieClip(clickedBoxes[index]).visivle = true;
}