Swap Depths in AS3

Karessa
Karessa used Ask the Experts™
on
The code in the function  rollSection(evtObject:MouseEvent)  is intended to make the object in question come to the highest depth, but it doesn't work! Is it because the objects are buttons? If so, can I cast them as movie clips?


stop();

var thisDepth:Number;

//handle events for buttons… about, home, portfolio are labels names in timeline
stage1_btn.addEventListener(MouseEvent.CLICK, clickSection);
stage2_btn.addEventListener(MouseEvent.CLICK, clickSection);
stage3_btn.addEventListener(MouseEvent.CLICK, clickSection);
stage4_btn.addEventListener(MouseEvent.CLICK, clickSection);
stage5_btn.addEventListener(MouseEvent.CLICK, clickSection);
stage1_btn.addEventListener(MouseEvent.MOUSE_OVER, rollSection);
stage2_btn.addEventListener(MouseEvent.MOUSE_OVER, rollSection);
stage3_btn.addEventListener(MouseEvent.MOUSE_OVER, rollSection);
stage4_btn.addEventListener(MouseEvent.MOUSE_OVER, rollSection);
stage5_btn.addEventListener(MouseEvent.MOUSE_OVER, rollSection);
function clickSection(evtObj:MouseEvent) {
	trace("The "+evtObj.target.name+" button was clicked!");
	//go to the section clicked on...
	gotoAndStop(evtObj.target.name);
}

function rollSection(evtObject:MouseEvent) {
	trace("The "+evtObject.target.name+" button was rolled!");
	//bring to the highest depth
	this.setChildIndex(evtObject.target.name,this.numChildren-1);
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Did you try to use swapChildren?

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#swapChildren%28%29

Try



this.swapChildren(evtObject.target,  getChildAt(this.numChildren-1));

Author

Commented:
Thanks for your help!

Hm, when I use that line I get

#1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject

Do I need to cast it as a movie clip? I tried:

MovieClip(this).swapChildren(evtObject.target,  getChildAt(this.numChildren-1));

But that was no good.
Top Expert 2011
Commented:
No, both children must be DisplayObject;

swapChildren(child1:DisplayObject, child2:DisplayObject):void

var t1:DisplayObject = evtObject.target as DisplayObject;
var t2:DisplayObject = getChildAt(this.numChildren-1) as DisplayObject;

if(t1 == null || t2 == null)
     throw new Error("Invalid targets");

this.swapChildren(t1, t2);

Author

Commented:
So great! Thank you so much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial