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?
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
forloop in the function that loops through a specified display object container’s children. Upon each interation it checks the type for eitherCircleorSquaretypes 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.