Skip to main content
FlashTapper
Inspiring
November 23, 2009
Question

SwapDepths in ActionScript 3?

  • November 23, 2009
  • 2 replies
  • 2548 views

Hi all,

Having issues with swapping stacking order in the new version of flash. I am developing a drag n drop activity and the educators aren't really making it easy for me (no one really understands you can't use paragraphs for drag n drop activities...).

Anyway so when you roll over the drag object, you see a "tool tip" appear in a blue rounded square explaining the meaning of the drag object better.

There are paragraphs on the left of the stage where the draggable objects need to be matched with (on the black rounded rectangle next to the sentence).

Even though i have the "tool tip" movies on a layer on the top of the timeline - once a drag object has been placed into its correct target, it sits on top of everything - especially the other tool tips of drag objects that have yet to be matched with their correct targets.

I have tried the following code to action once the drag object has been placed:

parent.setChildIndex(this, parent.numChildren-1);

Sadly, it doesn't work at all it seems. Does anyone have a solution that may work? And i suppose it is hard trying to convey this message in text so I am attaching the FLA file to this post.

Thanks in advance,

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
November 23, 2009

Try using....

MovieClip(this.parent).setChildIndex(this, MovieClip(this.parent).numChildren-1);

FlashTapper
Inspiring
November 23, 2009

Sadly, still not working

I get this error and nothing about it has changed...

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@ef2bb51 to flash.display.MovieClip.

What i am struggling to understand is how an object which is physically placed on a layer below my "tool tip" layer can intrude on the layer above it??? How does the flash stacking order work? It used to work quite logically (i.e. Photoshop - top layer is TOP!)

Please - i need help! Got to hand over this project today - im flying to thailand for a well-earned break tomorrow.

kglad
Community Expert
Community Expert
November 23, 2009

the easiest way to move an object to the topmost of the parent's children is to add it to the parent after the other children have been added.

(if it's already a child of the parent, (re)-adding it will remove it from its current index to the topmost index.)

kglad
Community Expert
Community Expert
November 23, 2009

the easiest way to move an object to the topmost of the parent's children is to add it to the parent after the other children have been added.

(if it's already a child of the parent, (re)-adding it will remove it from its current index to the topmost index.)

FlashTapper
Inspiring
November 24, 2009

I ended up devising an alternative. LOL didn't really have the time to figure this one out today

.

I'm still having issues with understanding the parent-child relationship of my stage objects. Now i cannot properly reset my activity because the "dropped" drag objects appear in the same place when i tell the timeline to go back to the start. I think i know why this is: the dropped objects are all child objects that have been added...

function pickUp(event:MouseEvent):void {
    event.target.startDrag(true);
    trace("object has been picked up");
    event.target.parent.addChild(event.target);
    startX = event.target.x;
    startY = event.target.y;
}

function dropIt(event:MouseEvent):void {
    event.target.stopDrag();
    var myTargetName:String = "target" + event.target.name;
    var myTarget:DisplayObject = getChildByName(myTargetName);
    if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
       trace("Good Job!");
        event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
        event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
        event.target.buttonMode = false;
        event.target.x = myTarget.x;
        event.target.y = myTarget.y;
  counter++;
    } else {
        trace("Sorry, try again!");
        event.target.x = startX;
        event.target.y = startY;
    }
    if(counter == 8){
        trace("Congrats, you're finished!");
  play();
    }
}


Now I understand the convenience of short re-usable code - but honestly i find i have more control if i make explicit functions for every single drag and target object. Is there any easy way to eliminate the childNode of each drag object when i make a reset button to go back to the start of the timeline?

Thanks,

Ryan