Below is a simple example in which we need to control the depth of a dragged square. Specifically, we want the dragged square to appear in front of other squares.
Download
- Flash CS3 file depths_3.fla
The Code Relevant to Depths
We refer you to our Tour of Drag and Drop Techniques in Flash CS3 tutorial in the Basic section for detailed discussion of dragging in AS3. Below, we only comment on the depth issue. (And digress into the type casting issue.)
................
block1.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
/*
In startBlockMove listener, we set the depth of the square being dragged
to this.numChildren-1. "this" stands for the MainTimeline which is the parent
of all squares. We use this.setChildIndex method. Since all children of the MainTimeline
are placed on the depths 0,1,..., this.numChldren-1, this.numChildren-1 is the highest
possible depth. Hence, the square being dragged will appear on top.
Note, that we use type casting: Sprite(evt.currentTarget) when addressing evt.currentTarget.
We are telling the compiler to treat evt.currentTarget (a square being dragged) as
a Sprite. Otherwise, in the Strict Mode, we will get a compiler error.
*/
function startBlockMove(evt:MouseEvent):void {
movingBlock = Sprite(evt.currentTarget);
this.setChildIndex(Sprite(evt.currentTarget),this.numChildren-1);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
}
................













