Flexcomps’s Weblog

Removing a child from a parent by passing instance name as string

Posted by: Yogesh Puri on: October 21, 2008

Following is a code to remove a child from a parent by passing it’s instance name as a string and parent as a display object

import fl.controls.Button;

//———– Draw a graphics on the stage ———-
var t:Sprite = new Sprite();
t.name = “testSprite”;
t.graphics.lineStyle(1,0×0000ff);
t.graphics.beginFill(0xff0000);
t.graphics.drawCircle(0,0,50);
t.graphics.endFill();
addChild(t);
t.x =100;
t.y = 100;

//———– Remove Child Function ————–
function removeChildWithRef(spriteName:String, parentObj:*){
var t:DisplayObject = parentObj.getChildByName(spriteName);
parentObj.removeChild(t);
}

//————- Add  a Button to Stage ————
var tBtn:Button = new Button();
tBtn.label = “Remove Circle”;
tBtn.x = 50;
tBtn.y = 170;
tBtn.addEventListener(MouseEvent.CLICK,onClick);
addChild(tBtn);

//———— Capture Click Event ————-
function onClick(evt:MouseEvent){
removeChildWithRef(“testSprite”,this);
}

4 Responses to "Removing a child from a parent by passing instance name as string"

var obj:DisplayObject;

if (obj.parent) obj.parent.removeChild(obj);

You leave me no choice but to call child protective services in AS4.
if (parent has child && !parent) removeChild from parent.

; )

[...] Visit link: Removing a child from a parent by passing instance name as string [...]

Thank you so much, man. Your function saved me.

Leave a Reply

 

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Blog Stats

  • 127,167 hits