如何使用actionscript 3隐藏除特定一个子剪辑之外的所有子剪辑

时间:2012-03-02 11:00:30

标签: actionscript-3

如何使用actionscript 3隐藏除特定部分之外的所有子剪辑?

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

for (var i:int = clip.numChildren - 1; i >= 0; i--) {
    clip.getChildAt(i).visible = false;
}

在此链接中找到:Hide all children of CS5 object in AS3 code

答案 1 :(得分:-1)

这是我制作的方法,应该让事情更简单。你可能想要打包它,因为它有相当多的代码,但好的是它的可重用性:

Main.as(文档类)

package 
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            // create a display object container then add 10
            // child display objects.

            var displayObjectContainer:Sprite = new Sprite();

            for (var i:int = 0; i < 10; i++)
            {
                var displayObject:Sprite = new Sprite(); 
                displayObject.name = "displayObject" + i;
                displayObjectContainer.addChild(displayObject);

            }// end for

            // examples :

            // hideAllDOInDOC(displayObjectContainer, exceptDisplayObjects:String);
            /*
            hideAllDOInDOC(displayObjectContainer, "displayObject8");
            var displayObject1:DisplayObject = 
                displayObjectContainer.getChildByName("displayObject1");
            var displayObject8:DisplayObject = 
                displayObjectContainer.getChildByName("displayObject8");
            trace(displayObject1.visible); // output: false
            trace(displayObject8.visible); // output: true
            */

            // hideAllDOInDOC(displayObjectContainer, exceptDisplayObjects:int);
            /*
            hideAllDOInDOC(displayObjectContainer, 7);
            var displayObject2:DisplayObject = 
                displayObjectContainer.getChildAt(2);
            var displayObject7:DisplayObject = 
                displayObjectContainer.getChildAt(7);
            trace(displayObject2.visible); // output: false
            trace(displayObject7.visible); // output: true
            */

            // hideAllDOInDOC(displayObjectContainer, exceptDisplayObjects:Vector.<String>);

            hideAllDOInDOC(displayObjectContainer, 
                           Vector.<String>(["displayObject4", "displayObject6"]));
            var displayObject2:DisplayObject = 
                displayObjectContainer.getChildByName("displayObject2");
            var displayObject4:DisplayObject = 
                displayObjectContainer.getChildByName("displayObject4");
            var displayObject6:DisplayObject = 
                displayObjectContainer.getChildByName("displayObject6");
            trace(displayObject2.visible); // output: false
            trace(displayObject4.visible); // output: true
            trace(displayObject6.visible); // output: true


            // hideAllDOInDOC(displayObjectContainer, exceptDisplayObjects:Vector.<int>);
            /*
            hideAllDOInDOC(displayObjectContainer, 
                           Vector.<int>([4, 6]));
            var displayObject2:DisplayObject = 
                displayObjectContainer.getChildAt(2);
            var displayObject4:DisplayObject = 
                displayObjectContainer.getChildAt(4);
            var displayObject6:DisplayObject = 
                displayObjectContainer.getChildAt(6);
            trace(displayObject2.visible); // output: false
            trace(displayObject4.visible); // output: true
            trace(displayObject6.visible); // output: true
            */

        }// end function

        // hideAll display objects in display object container
        private function hideAllDOInDOC(displayObjectContainer:DisplayObjectContainer,
                                        exceptDisplayObjects:Object):void
        {

            // if exceptDisplayObjects parameter is not of type
            // String, int, Vector.<String> or Vector.<int> then
            // throw ArgumentError

            if (!(exceptDisplayObjects is String) &&
                !(exceptDisplayObjects is int) &&
                !(exceptDisplayObjects is Vector.<String>) &&
                !(exceptDisplayObjects is Vector.<int>))
            {
                throw new ArgumentError(
                    "exceptDisplayObjects parameter must be either type String, int, Vector.<String> or Vector.<int>")

            }// end if

            // loop through display object container's children

            for (var i:int = 0; i < displayObjectContainer.numChildren; i++)
            {
                var displayObject:DisplayObject = displayObjectContainer.getChildAt(i);
                displayObject.visible = false; // hide display object

                if (exceptDisplayObjects is String)
                {
                    // reveal display object if it's name is the same as
                    // exceptDisplayObjects string value

                    if (displayObject.name == (exceptDisplayObjects as String))
                    {
                        displayObject.visible = true;

                    }// end if

                }
                else if(exceptDisplayObjects is int)
                {
                    // reveal display object if it's index(i) is the same as
                    // exceptDisplayObjects integar value

                    if (i == (exceptDisplayObjects as int))
                    {
                        displayObject.visible = true;

                    }// end if

                }
                else 
                {
                    if(exceptDisplayObjects is Vector.<String>)
                    {
                        for (var j:int = 0; j < (exceptDisplayObjects as Vector.<String>).length; j++)
                        {
                            // reveal display object if it's name is the same as
                            // exceptDisplayObjects[j] string value

                            if (displayObject.name == (exceptDisplayObjects[j] as String))
                            {
                                displayObject.visible = true;

                            }// end if

                        }// end for

                    }
                    else if(exceptDisplayObjects is Vector.<int>)
                    {
                        for (var k:int = 0; k < (exceptDisplayObjects as Vector.<int>).length; k++)
                        {
                            // reveal display object if it's index(i) is the same as
                            // exceptDisplayObjects[k] integar value

                            if (i == (exceptDisplayObjects[k] as int))
                            {
                                displayObject.visible = true;

                            }// end if

                        }// end for

                    }// end else if

                }// end 

            }// end for

        }// end function

    }// end class

}// end package

使用hideAllDOInDOC()方法,您可以根据名称或索引指定显示对象容器中哪些显示对象不隐藏。您还可以使用Vector.<String>Vector.<int>指定一组不会根据名称或索引隐藏的显示对象。以下是一些例子:

hideAllDOInDOC(displayObjectContainer, "displayObject8");

hideAllDOInDOC(displayObjectContainer, 7);

hideAllDOInDOC(displayObjectContainer, 
    Vector.<String>(["displayObject4", "displayObject6"]));

hideAllDOInDOC(displayObjectContainer, 
    Vector.<int>([4, 6]));