| April 1st, 2008 |
Está en una clase simple pero muy útil, al menos para mí, que recorre todos los hijos contenidos en un DisplayObject, la clase cuenta con 2 funciones:
getAllChilds
Regresa todos los hijos contenidos en el “target”
getChildsByType
Regresa solo los hijos que sean de un tipo específico, por ejemplo solo MovieClips, o solo Botones.
DisplayObjectUtils
Actionscript:
-
package net.tmeister.utils
-
{
-
/**
-
* @author Enrique Chavez aka Tmeister
-
*/
-
import flash.display.DisplayObject;
-
public class DisplayObjectUtils
-
{
-
/**
-
*
-
* @param target
-
* @return Array
-
*/
-
public static function getAllChilds(target:*):Array
-
{
-
var listTmp:Array = []
-
for (var a = 0; a <target.numChildren; a++ )
-
{
-
listTmp.push (target.getChildAt(a) )
-
}
-
return listTmp
-
}
-
/**
-
*
-
* @param target
-
* @param type
-
* @return
-
*/
-
public static function getChildsByType(target:*, type:*):Array
-
{
-
var listTmp:Array = []
-
for (var a = 0; a <target.numChildren; a++ )
-
{
-
if (target.getChildAt(a) is type)
-
{
-
listTmp.push (target.getChildAt(a) )
-
}
-
}
-
return listTmp
-
}
-
}
-
}
Su uso es el siguiente:
Primero creamos unos Movieclips y unos botones para tener algo que buscar.
Actionscript:
-
import net.tmeister.utils.DisplayObjectUtils;
-
import fl.controls.Button
-
-
createMovieClips()
-
createButtons()
-
getChilds()
-
-
function createButtons()
-
{
-
for(var a:Number = 0; a<2; a++)
-
{
-
var tmp:Button = new Button();
-
tmp.x = Math.random()*400
-
tmp.y = Math.random()*400
-
tmp.label = "buton"+a
-
addChild(tmp)
-
}
-
}
-
function createMovieClips()
-
{
-
for(var a:Number = 0; a<5; a++)
-
{
-
var tmp:MovieClip = new MovieClip();
-
tmp.graphics.beginFill(0x2a2a2a, .5)
-
tmp.graphics.drawRect(Math.random()*500, Math.random()*300, Math.random()*100, Math.random()*100);
-
tmp.graphics.endFill();
-
tmp.name = "mc"+a;
-
addChild(tmp)
-
}
-
}
-
function getChilds()
-
{
-
trace("All Childs: " + DisplayObjectUtils.getAllChilds(this) )
-
trace("=====================================================================================")
-
trace("Movieclips: " + DisplayObjectUtils.getChildsByType(this, MovieClip))
-
trace("=====================================================================================")
-
trace("Buttons: " + DisplayObjectUtils.getChildsByType(this, Button))
-
-
}
|