Child Sprites – Handling depth, removing and more
Posted on 30. Mar, 2009 by thornyeternity in ActionScript 3 Snippets
Determine amount of children an object has:
for(var i:Number = 0; i < containerSprite.numChildren ; i++)
————-
Determine last added child object:
var maxKids:Number = containerSprite.numChildren-1;
containerSprite.getChildAt(maxKids).visible = true;
————
Should one need to use parent to go back up:
private function cancelDownload(event:MouseEvent):void{
//trace(event.target.parent.parent.name);//parent was the button itself so must go up by 2
removeChild(Sprite(event.target.parent.parent)); //convert whatever it is to Sprite
}//cancelDownload
Or if object is nested in a container:
uploadInfoContainer.removeChild(Sprite(event.target.parent.parent)); //convert whatever it is to Sprite
————
To remove a sprite or added object and not get an error, check it exists:
if(loaderCaseStudyBig != null){
deleteCaseSprite.removeChild(loaderCaseStudyBig);
}
————
To remove a sprite or added object in a function where output may give ‘child of caller’ error, use a Boolean to check when or how the remove is activated:
if(loaderCaseStudyBig != null && doRemove == true){
deleteCaseSprite.removeChild(loaderCaseStudyBig);
}
————
Toggle child depth – one could toggle visibility instead, depending on the case:
private function toggleSlideShow(event:MouseEvent):void{
slideGoing = !slideGoing; //swop variable true<=>false value on click
slideContainerSprite.swapChildren(slidePlaySprite , slidePauseSprite);
if(slideGoing == false){
myTimer.removeEventListener(TimerEvent.TIMER, updateDisplay);
myTimer.stop();
}else{
myTimer.addEventListener(TimerEvent.TIMER, updateDisplay);
myTimer.start();
}
}//toggleSlideShow
———–
Bring clicked sprite to front of stacking order:
for(var i:Number = 0; i < containerSprite.numChildren ; i++){
containerSprite.setChildIndex(Sprite(event.target) , containerSprite.numChildren – 1);
}
———-
Send clicked sprite to back of stacking order:
containerSprite.setChildIndex(Sprite(event.target) , 0);
———
Send top sprite in stacking order to the back:
containerSprite.setChildIndex(containerSprite.getChildAt(containerSprite.numChildren-1) , 0);
——–
Reparenting – moving a sprite from being nested in one sprite to being nested in another:
receiverSprite.addChild(Sprite(event.target));
//where event.target was the child of containerSprite at time of clicking
//it will then be a child of receiverSprite and inherit that sprite’s properties
——–
Custom Events for new sprites using same function:
addHandles(“cropTopHandle”);
addHandles(“cropBottomHandle”);
addHandles(“cropLeftHandle”);
addHandles(“cropRightHandle”);
private function addHandles(newName:*):void{
trace(newName); //TRACES: cropBottomHandle
var currentOne:String = newName;
newName = new Sprite();
cropSprite.addChild(newName);
doHandleFill(newName);
trace(newName);//TRACES: [Object Sprite]
//newName could just be a string with a different var used for the new sprite(s) actually
//this is probably piffle but interesting for untyped variables and how they can mutate
if(currentOne == “cropBottomHandle”){
newName.addEventListener(MouseEvent.MOUSE_DOWN, resizeBottomCropArea);
}//if
}//addHandles
——————-
Get depth of object clicked on:
trace(containerSprite.getChildIndex(Sprite(event.target)));
——————–
Add child behind everything:
addChildAt(theBackground,0); (the thing to add, its depth)
———————-
For movie clip in physical timeline of dynamically added sprites. Prevent ‘underfined property’ error by:
var currMainSprite:Sprite = Sprite(containerSprite.getChildAt(checkComplete));//get current sprite
currMainSprite.getChildByName(“preloader_mc”).visible = false;


