gpt4 book ai didi

actionscript-3 - AS3 获取舞台上某个类的所有实例的数组?

转载 作者:行者123 更新时间:2023-12-04 04:33:14 25 4
gpt4 key购买 nike

假设我已经定义了 myCircle 类以及所有这些。如果我的代码如下:

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)

如何编写一个函数来自动返回[circle1,circle 2,circle3,circle4]数组?

最佳答案

很简单,看看我做的以下内容:

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

这里的三个关键函数是 getCircles()、getSquares() 和 getChildren()。它们本质上都做同样的事情,函数中有一个 for 循环,循环遍历指定的显示对象容器的子级。每次交互时,它都会检查 getCircles()getSquares() 中的 CircleSquare 类型分别函数,然后将每个显示对象添加到函数返回的本地数组中。

getChildren() 函数允许预先指定类型,从而更进一步。

关于actionscript-3 - AS3 获取舞台上某个类的所有实例的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com