Tuesday, April 21, 2009

GetChildByName

This is nice. If you have "parts" (children) of a movie clip that you want to show as the result of some event (mouse overs, mouse clicks, button clicks), then you need to give the children of the movie clips names. i.e. :

private function createCircle(x:Number, y:Number, r:Number):Sprite
{
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xcccccc, 0.5);
sprite.graphics.lineStyle(1, 0x000000);
sprite.graphics.drawCircle(0, 0, r);
sprite.x = x;
sprite.y = y;
var textField:TextField = new TextField();
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFF0000;
textFormat.size = 12;
textField.name = "Larry";
textField.visible = false;
textField.text = "[" + x.toString() + ", " + y.toString() + "]";
textField.y=-50;
textField.setTextFormat(textFormat);
sprite.addChild(textField);
sprite.addEventListener(MouseEvent.MOUSE_OVER, showText);
sprite.addEventListener(MouseEvent.MOUSE_OUT, noText);
return sprite;
}

private function showText(event:MouseEvent):void {
event.target.getChildByName("Larry").visible =true; }

private function noText(event:MouseEvent):void {
event.target.getChildByName("Larry").visible = false; }

0 comments: