如何检查actionscript中是否存在按钮

时间:2014-02-23 11:56:14

标签: flash button actionscript

我是动作脚本的新手。如何检查actionscript中是否存在按钮。我猜想使用removechildname函数,尝试没有成功。请帮帮我。

2 个答案:

答案 0 :(得分:0)

也许这样的事情会帮助你:

// Since you have a array with names, i think you can do this:

// Get the length
var lengthOfButtonsNamesArray:int = buttonsNamesArray.length;
// looop through your array of names
for(var index:int = 0; index < lengthOfButtonsNamesArray; ++index){
    // getting the button inside your buttons container using the array of names and getChildByName Method
    var button:DisplayObject = buttonsContainer.getChildByName(buttonsNamesArray[index]) as DisplayObject;
    // if the button was found
    if(button != null){
        // remove it
        buttonsContainer.removeChild(button);
    }
}

///////////////////////
// OR
//////////////////////

// If you are sure that there are only buttons inside your buttons container and you want to remove all of them 

// Check the amount of children inside your buttons container
// while its greater then zero, remove the child at index of zero
while(buttonsContainer.numChildren > 0){
    buttonsContainer.removeChildAt(0);
}

我没有测试它,但我很确定这是有效的,无论如何让我知道你是怎么做的。

答案 1 :(得分:0)

使用contains()

if (this.contains(myButton))
{
    // then we know myButton exists inside of this class
    this.removeChild(myButton); // removeElement(), in some situations
}

if (myDisplayContainer.contains(myButton))
{
    // then we know myButton exists inside of myDisplayContainer
    myDisplayContainer.removeChild(myButton); // removeElement(), in some
                                              // situations
}