简体   繁体   English

AS3在舞台上获取类的所有实例的数组?

[英]AS3 get an array of all instances of a class on a stage?

Assume I have the myCircle class all defined and all that. 假设我已经定义了myCircle类以及所有这些。 If my code is as follows: 如果我的代码如下:

var circle1:myCircle = new myCircle()
var circle2:myCircle = new myCircle()
var circle3:myCircle = new myCircle()
var circle4:myCircle = new myCircle()

stage.addChild(circle1)
stage.addChild(circle2)
stage.addChild(circle3)
stage.addChild(circle4)

How would I write a function to return an array of [circle1, circle 2, circle3, circle4] automatically? 我将如何编写一个函数以自动返回[circle1,circle 2,circle3,circle4]的数组?

It's simple, take a look at the following I made: 很简单,请看一下我所做的以下工作:

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);

            for (var i:uint = 0; i < 5; i++)
            {
                var circle:Circle = new Circle();
                circle.name = "circle" + (i+1);
                stage.addChild(circle);

            }// end for

            for (var j:uint = 0; j < 5; j++)
            {
                var square:Square = new Square();
                square.name = "square" + (j+1);
                stage.addChild(square);

            }// end for

            traceNames(getCircles(stage)); // output: circle1
                                           //         circle2
                                           //         circle3
                                           //         circle4
                                           //         circle5

            traceNames(getSquares(stage)); // output: square1
                                           //         square2
                                           //         square3
                                           //         square4
                                           //         square5


            traceNames(getChildren(stage, Circle)); // output: circle1
                                                    //         circle2
                                                    //         circle3
                                                    //         circle4
                                                    //         circle5

        }// end function

        private function getCircles(container:DisplayObjectContainer):Array
        {
            var array:Array = new Array();

            for (var i:uint = 0; i < container.numChildren; i++)
            if (container.getChildAt(i) is Circle)
            array.push(container.getChildAt(i));

            return array;

        }// end function

        private function getSquares(container:DisplayObjectContainer):Array
        {
            var array:Array = new Array();

            for (var i:uint = 0; i < container.numChildren; i++)
            if (container.getChildAt(i) is Square)
            array.push(container.getChildAt(i));

            return array;

        }// end function

        private function getChildren(container:DisplayObjectContainer, type:Class):Array
        {
            var array:Array = new Array();

            for (var i:uint = 0; i < container.numChildren; i++)
            if (container.getChildAt(i) is type)
            array.push(container.getChildAt(i));

            return array;

        }// end function

        private function traceNames(array:Array):void
        {
            for each(var displayObject:DisplayObject in array)
            trace(displayObject.name);

        }// end function

    }// end class

}// end package

import flash.display.Sprite;

internal class Circle extends Sprite
{
    public function Circle() {}

}// end class

internal class Square extends Sprite
{
    public function Square() {}

}// end class

The three key functions here are getCircles(), getSquares() and getChildren(). 这里的三个关键函数是getCircles(),getSquares()和getChildren()。 They all essentially do the same thing, theres a for loop in the function that loops through a specified display object container's children. 它们本质上都做相同的事情,函数中有一个for循环,该循环遍历指定显示对象容器的子级。 Upon each interation it checks the type for either Circle or Square types in the getCircles() and getSquares() functions respectively, and then it adds each display object to a local array which is returned by the function. 每次插入时,它都会分别检查getCircles()getSquares()函数中CircleSquare类型的类型,然后将每个显示对象添加到函数返回的本地数组中。

The getChildren() function takes things a step further by allowing for the type to be specified beforehand. 通过允许预先指定类型, getChildren()函数使事情更进一步。

I'm not going to do your job for you but I can give you a hint: 我不会为您完成工作,但可以给您一个提示:

you can check if something is a myCircle instance by doing 您可以通过执行以下操作来检查某物是否是myCircle实例:

if(child is myCircle)

so when you loop through all children of the stage you can put the children that ARE instances of myCircle into the array and if not, do nothing. 因此,当您遍历该阶段的所有子级时,可以将作为myCircle实例的子级放入数组中,如果没有,则不执行任何操作。 That will give you an array of all children that are myCircles. 这将为您提供所有属于myCircles的孩子的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM