在as3

时间:2016-07-17 17:15:05

标签: actionscript-3

我有一个带有六个按钮的数组,称为“sostaletters”,另一个数组包含六个名为“addedletters”的元素。我想要的是每次点击一个按钮,从阵列“addedletters”中添加一个新的动画片段,以便在舞台上添加。例如,如果单击数组“sostaletters”的第三个元素,则数组的第三个元素“addedletters”将添加到舞台上。我怎么能这样做?

这就是我为我的阵列“sostaletters”所做的事情

 var sostaletters:Array = [a7,a17,a24,a1,a18] 
    for each (var letter:MovieClip in sostaletters) {
        letter.buttonMode = true;
        letter.isClicked = false;
        letter.addEventListener(MouseEvent.CLICK, kanoklick2);
        function kanoklick2(event:MouseEvent):void
{
        event.target.alpha = 0.5;
        if(event.currentTarget.isClicked == false){
        clickCount2 ++;
        event.currentTarget.isClicked = true;
        sostaletters[i].x = positionsArray[i].xPos;
        sostaletters[i].y = positionsArray[i].yPos;
        setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}
if(clickCount2 == sostaletters.length){
    addChild(welldoneman);
    myTimer.start();
       if (contains(kremmala)) {
        removeChild(kremmala)
        }
for (var i:int= 0; i< wrongletters.length; i++) 
{
    wrongletters[i].removeEventListener(MouseEvent.CLICK, kanoklick);
 }
for (var o:int= 0; o< sostaletters.length; o++) 
{
    sostaletters[o].removeEventListener(MouseEvent.CLICK, kanoklick2);
}
trace("All buttons have been clicked");

}
}
}

1 个答案:

答案 0 :(得分:0)

Neal Davis已经给出了正确答案。但我会进行代码审查以获得更清晰的信息。请参阅我在代码中的评论。

var sostaletters:Array = [a7,a17,a24,a1,a18] 
// "sostaletters" is an Array. So it is better to use iteration by index
for each (var letter:MovieClip in sostaletters) {
   letter.buttonMode = true;
   letter.isClicked = false;
   letter.addEventListener(MouseEvent.CLICK, kanoklick2);

   // You will create "kanoklick2" function at each iteration. 
   // Move this declaration from this loop
   function kanoklick2(event:MouseEvent):void {
     event.target.alpha = 0.5;

     // There is no need to compare a boolean property with "false" or "true"
     // You can write "if(event.currentTarget.isClicked)"
     if(event.currentTarget.isClicked == false) {
       clickCount2 ++;
       event.currentTarget.isClicked = true;

       // You declare an "i" variable below. 
       // So the compiler will not fire en error.
       // But you will not get an index of the current letter.
       sostaletters[i].x = positionsArray[i].xPos;
       sostaletters[i].y = positionsArray[i].yPos;
       setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
     }

     // If number of "sostaletters" are not changed
     // you can save its value in a variable for an example "sostalettersNum"
     if (clickCount2 == sostaletters.length) {
       addChild(welldoneman);
       myTimer.start();
       if (contains(kremmala)) {
         removeChild(kremmala)
       }

       // You declare an "i" variable. And use it above to get a current letter. 
       // But an "i" will not contain its index. 
       // It will be equal to "wrongletters.length"
       for (var i:int= 0; i