Reparenting in ActionScript 3

We use a modification of the fla file from the first page to illustrate reparenting of display objects in ActionScript 3 and implications of changing parents regarding the way objects respond to events.

This time when the movie opens the Sripte spDisk is a child of spBoard. Even though, by default, spDisk.mouseEnabled=true, clicks on spDisk are registered as spDisk is a child of spBoard.

 

var spBoard:Sprite = new Sprite();

spBoard.graphics.lineStyle(0,0x333333);

spBoard.graphics.beginFill(0xFFFF99);

spBoard.graphics.drawRect(0,0,200,200);

spBoard.graphics.endFill();

 

var spDisk:Sprite = new Sprite();

spDisk.graphics.lineStyle(0,0x000000);

spDisk.graphics.beginFill(0x660000);

spDisk.graphics.drawCircle(0,0,40);

spDisk.graphics.endFill();

 

this.addChild(spBoard);

spBoard.x = 50;

spBoard.y = 65;

 

spBoard.addChild(spDisk);

spDisk.x = 120;

spDisk.y = 120;

 

.......................................

Reparenting in ActionScript 3 is very simple: you assign the object that you want to reparent to the new parent via:
newParent.addChild(objectToReparent);
The object is added to the new parent and automatically removed as a child of its old parent.

 

/*
Reparent button assigns spDisk as a child of the MainTimeline. When spDisk is assigned as a child of the MainTimeline, it automatically is removed from the list of children of spBoard. Hence, all you need to do to reparent an object is to assign it to be a child of a new DisplayObjectContainer. When spDisk is no longer a child of spBoard, portions of spBoard underneath it will no longer be notified of mouse clicks. Only the top InteractiveObject receives a notification; that is, spDisk.
*/

 

butReparent.addEventListener(MouseEvent.CLICK,reparentClicked);

 

function reparentClicked(evt:MouseEvent):void {

this.addChild(spDisk);

}

 

When you click Reparent button (or Reset button) the position of spDisk changes as the meaning of its x and y coordinates set above changes. The coordinates are relative to the parent object.

/*
Disable button disables spDisk as a recipient of mouse events. After disabling spDisk, clicks on it are registered by spBoard.
*/

 

butDisable.addEventListener(MouseEvent.CLICK,disableClicked);

 

function disableClicked(evt:MouseEvent):void {

spDisk.mouseEnabled=false;

}

 

butReset.addEventListener(MouseEvent.CLICK,resetClicked);

 

/*
Reset button reparents spDisk back to be a child of spBoard, enables spDisk, and resets the number of clicks.
*/

 

function resetClicked(evt:MouseEvent):void {

spBoard.addChild(spDisk);

clicksCounter=0;

numClicks.text=String(clicksCounter);

spDisk.mouseEnabled=true;

}

 

Download

The fla file linked below contains complete, commented source code for the Flash movie above.

Back to Intermediate Tutorials              Back to Flash and Math Home

The site www.flashandmath.com is maintained by Doug Ensley (doug@flashandmath.com) and Barbara Kaskosz (barbara@flashandmath.com).
It has been developed with partial funding from the National Science Foundation and the Mathematical Association of America.