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



假设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;
}

最新更新