如何在 for 循环 as3 中单独访问 Movieclips

How to access Movieclips individually in a for loop as3

假设 mySaveNewT.data.myNText = 20,在 for 循环中,舞台上填充了 20 个 MovieClips (tbox)。单击 tbox 实例时,我想将其可见性更改为 false

  1. 如何引用被点击的单个 MovieClip,而不必将每个 MovieClip 的可见性都设置为 false? (即如果 MC[2]MC[10] 被点击,但其余的没有)

  2. 如何将其放入数组?

这是我的 for 循环:

    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);
    }

要推入数组,并添加点击侦听器和更改可见性,请参阅代码注释:

//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;
}