ActionScript 3.0: how to use the getChildByName() method of the DisplayObjectContainer class
short intro:
working on a very simple task the other day, I ended up using the getChildByName method of the DisplayObjectContainer class.
first of all, to get a better understanding of what I was working on, check out this:
my very simple Flash without the use of getChildByName
the problem:
clicking one of the polaroids will make it fade in in a bigger version, clicking that bigger version will make it fade out again.
the problem is, the more photos you click, the stranger the bigger version will look as it fades out, because you see every picture loaded in big versions so far fading out.
the solution:
what happens when one of the polaroids is clicked is, that I load the picture, name the loader “myLoader”, and add as a child to the DisplayObject containing the white frame and the shadow.
the trick now is to check if the DisplayObject has any children name “myLoader”. if it has, I’ll remove them before adding the new myLoader, check it out:
my very simple Flash with the use of getChildByName
the ActionScript:
eager to get the code for this, well, just grab the following.
the getChildByName part of the code is highlighted with comments in the code
import fl.motion.easing.*;
import flash.display.Sprite;
import flash.display.DisplayObject;
var thePic:Loader;
var tween:GTween;
allSmalls.small1.buttonMode = true;
allSmalls.small2.buttonMode = true;
allSmalls.small3.buttonMode = true;
allSmalls.addEventListener(MouseEvent.CLICK, showBig, false, 0, true);
picHolder.alpha = 0;
picHolder.y -= 800;
function showBig(e:Event):void {
allSmalls.removeEventListener(MouseEvent.CLICK, showBig);
allSmalls.mouseChildren = false;
//THIS IS THE SPECIAL GETCHILDBYNAME PART
if(picHolder.getChildByName("thePic")){
var deleteThis:DisplayObject = picHolder.getChildByName("thePic");
picHolder.removeChild(deleteThis);
}
//END OF GETCHILDBYNAME PART
thePic = new Loader();
thePic.x = 11;
thePic.y = 8;
thePic.name = "thePic";
thePic.contentLoaderInfo.addEventListener(Event.COMPLETE, fadeBigIn, false, 0, true);
var theNameOfPic:String = e.target.name;
theNameOfPic = theNameOfPic.slice(-1);
theNameOfPic = "big"+theNameOfPic+".jpg";
thePic.load(new URLRequest(theNameOfPic));
}
function fadeBigIn(e:Event):void {
thePic.removeEventListener(Event.COMPLETE, fadeBigIn);
picHolder.buttonMode = true;
picHolder.addEventListener(MouseEvent.CLICK, hidePicHolder, false, 0, true);
picHolder.addChild(thePic);
tween = new GTween(picHolder, .6, {alpha:1}, {ease:Linear.easeNone});
picHolder.y += 800;
}
function hidePicHolder(e:MouseEvent):void {
picHolder.buttonMode = false;
allSmalls.mouseChildren = true;
allSmalls.addEventListener(MouseEvent.CLICK, showBig, false, 0, true);
tween = new GTween(picHolder, .6, {alpha:0}, {ease:Linear.easeNone, onComplete:moveUp});
}
function moveUp(e:GTween):void {
picHolder.y -= 800;
}
credits:
the ActionScript 3.0 Language Reference on getChildByName.
the wonderful street performers in New Orleans are photographed by Jason Winkler from Houston, Texas.
Stig Meyer Jensen for being a fellow geek.