AS3 获取舞台上某个类的所有实例的数组?
假设我已经定义了 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] 数组?
Assume I have the myCircle class all defined and all that. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很简单,看一下我做的以下内容:
这里的三个关键函数是 getCircles()、getSquares() 和 getChildren()。它们本质上都做同样的事情,函数中有一个 for 循环,循环遍历指定的显示对象容器的子级。每次交互时,它都会检查
getCircles()
和getSquares()
中的Circle
或Square
类型分别函数,然后将每个显示对象添加到函数返回的本地数组中。getChildren() 函数更进一步,允许预先指定类型。
It's simple, take a look at the following I made:
The three key functions here are getCircles(), getSquares() and 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. Upon each interation it checks the type for eitherCircle
orSquare
types in thegetCircles()
andgetSquares()
functions respectively, and then it adds each display object to a local array which is returned by the function.The
getChildren()
function takes things a step further by allowing for the type to be specified beforehand.我不会为你做你的工作,但我可以给你一个提示:
你可以通过这样做来检查某物是否是 myCircle 实例,
当你循环遍历舞台的所有子项时,你可以将作为 myCircle 实例的子项放入进入数组,如果没有,则不执行任何操作。这将为您提供 myCircles 中所有子项的数组。
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
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. That will give you an array of all children that are myCircles.