在actionscript3中添加/删除数组的侦听器

时间:2013-12-21 13:04:00

标签: arrays actionscript-3 button for-loop output

早上好!

我目前正在制作一个制作刽子手游戏的项目。我使用动画按钮代替键盘输出游戏中的不同字母。但是我遇到的最大和唯一的问题是这些按钮不会被识别为它们输出的值。如果我在“addButtonListeners”函数中跟踪array [i],我得到的每个按钮都是“[object SimpleButton]”。动画中的每个按钮都被命名为A,B,C等。

现在我想问你。你有解决我的问题,或不同的解决方案。我添加了您需要的必要代码。非活动变量工作正常。问题在于,由于无法识别按钮,因此无法正确放置非活动对象。提前谢谢!

    var inactive:Inactive;//Blocks buttons already pressed
    var icontainer:Array = new Array();//Array to save inactive buttons for easy removal 
    var keys:int = 0;//Count buttons that are inactive, this indicates how many "icontainer" that must be deleted. 
    var index:int;//Saves the value of String.search method, used to search after button pressed
    var buttons:Array;//Array with all the buttons


    buttons = new Array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,Æ,Ø,Å);


function init(e:MouseEvent)
{ 
    addButtonListeners(buttons);//Adds listeners to the buttons 
    ...
}


function addButtonListeners(array:Array):void
{   
    for (var i:int = 0; i < array.length; i++) 
    {
    array[i].addEventListener(MouseEvent.CLICK, onKeyPress);
    }
}


function removeButtonListeners(array:Array):void 
{   
    for (var i:int = 0; i < array.length; i++) 
    {
    array[i].removeEventListener(MouseEvent.CLICK, onKeyPress);
    }
}

function onKeyPress(e:MouseEvent):void 
{ 
    /* Gets and deactivates button */ 

    inactive = new Inactive();
    inactive.X = e.currentTarget.parent.X;//Added "parent" because flash recognizes the MC inside as "name"
    inactive.Y = e.currentTarget.parent.Y;

    addChild(inactive); 
    icontainer.push(inactive); 
    keys++; 

    /* Plays sound */ 

    btnSnd.play(); 

    /* Check if letter is in the word */ 

    index = currentWords.search(new String(e.currentTarget.parent.name)); 

    if (index != -1) 
    { 
        /* Letter is in the word */

        for (var i:int = 0; i < letters.length; i++)
        { 
            if (new String(e.target.parent.name) == letters[i]) 
            { 
                tfs[i].text = new String(e.target.parent.name); 

                correctLetters++; 
            } 
        }

        if (correctLetters + spaces == letters.length)//If word is complete 
        {
            win(); 
            removeButtonListeners(buttons); 
        }
    }
    else 
    { 
        tries--; 

        switch (tries)//Shows the hangman parts
        { 
            case 6 : 
            trace(6);
            break;

            case 5 :
            head.visible = true; 
            break;

            case 4 :
            body.visible = true;
            break;

            case 3 :
            leftArm.visible = true; 
            break;

            case 2 :
            rightArm.visible = true; 
            break;

            case 1 :
            leftLeg.visible = true; 
            break;

            case 0 :
            rightLeg.visible = true;
            for (var j:int = 0; j < letters.length; j++)//Shows the missing parts
            { 
                if (tfs[j].length == 0) 
                {
                    tfs[j].textColor = 0xFF0000; 
                    tfs[j].text = letters[j]; 
                }
            }

           removeButtonListeners(buttons); 
           fail();
           break;

           default :
           trace("Error"); 
        }
    }
}

0 个答案:

没有答案