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

时间:2015-05-26 20:48:37

标签: arrays actionscript-3 flash for-loop

让我们说class,并在for循环中,在舞台上填充了20个MovieClip(mySaveNewT.data.myNText = 20)。单击tbox实例后,我想将其可见性更改为tbox

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

  2. 如何将其推入阵列?

  3. 这是我的for循环:

    MC[10]

1 个答案:

答案 0 :(得分: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;
}