;

PDA

Click to See Complete Forum and Search --> : [RESOLVED] setChildIndex question- i know there's a simple solution i'm not seeing


krashe1313
07-01-2009, 12:18 PM
Hello,

I'm back and working on teaching myself more AS3 to expand on the stuff i've learned already and the great advice you've given me in the past, in the down between projects.

so here's what i'm trying to do. simple. making a small "game" as an exercise. I currently have 4 boxes (blue, red, green, purple) on the screen. Each an instance of box_mc, instance named "boxMC", "boxMC1", etc.

You can drag each of them around. That works.

My question is that when i click on a box, I would like to set the Z-order to the top most layer and could be dragged over, and let go, the others. I used the setChildIndex, but I can only apply it to just a particular instance...

setChildIndex(boxMC2, numChildren-1);

...but not dependent on which the box that is being clicked. This is what I'm currently trying, but with no luck:

function initDragger(mc:MovieClip):void
{
mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
setChildIndex(mc, numChildren -1);
//trace(numChildren);
}

function mouseDownHandler(event:MouseEvent):void
{
event.currentTarget.startDrag();

}
function mouseUpHandler(event:MouseEvent):void
{
event.currentTarget.stopDrag();
}

// Set up drag
initDragger(boxMC);
initDragger(boxMC1);
initDragger(boxMC2);
initDragger(boxMC3);

any help would be great! thank you very much in advance!
`shields

Mavrisa
07-01-2009, 01:47 PM
function mouseDownHandler(event:MouseEvent):void
{
setChildIndex(event.currentTarget, numChildren-1);
event.currentTarget.startDrag();
}

think that should solve your problem.

krashe1313
07-01-2009, 02:44 PM
function mouseDownHandler(event:MouseEvent):void
{
setChildIndex(event.currentTarget, numChildren-1);
event.currentTarget.startDrag();
}

think that should solve your problem.

tried that, but get this error:
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.

I've attached my file if it helps any. it's not fancy. just a learning file.

Mpjraaij
07-01-2009, 02:52 PM
This is an extract of my code: (The attached fla works as well)

// These items will get the drag functionality
addDrag(box);

private function addDrag($mc:MovieClip):void
{
$mc.addEventListener(MouseEvent.MOUSE_DOWN, doDrag);
$mc.addEventListener(MouseEvent.MOUSE_UP, killDrag);
}

private function doDrag(e:MouseEvent):void
{
var dragger = e.currentTarget;
dragger.startDrag(true);
this.setChildIndex(dragger, (this.numChildren - 1));
}
private function killDrag(event:MouseEvent):void
{
var dragger = e.currentTarget;
dragger.stopDrag();
}

krashe1313
07-01-2009, 03:41 PM
wow- excellent- thank you for the example and file. i really appreciate it! i'm looking it over right now!

thanks!