Search code examples
actionscript-3movieclipflash-cs6

Dragging and placing movieclips


I'm trying to make an animation using AS3 which consists of dragging and placing movieclips (small images) inside some rectangles (bitmaps) and then presenting the images (big images) corresponding to the movieclips by the same order as they were placed along the rectangles.

However, there is a little trick with which I'm having a few difficulties. When a movieclip is placed inside a rectangle and I try to drag another movieclip to the same rectangle, the one that I'm dragging should return to the initial position. The code I have is working sometimes, but others, it doesn't (still possible to place the movieclip above or under the other).

Another question is: How can I make for the movieclip that I'm dragging to always go above another movieclip and not under? (sometimes they go above, anothers they go under).

Thanks in advance. Best regards.

Here is my code:

function returnToInitial(k:int){
this["foto"+String(k)+"_mc"].x = this["foto"+String(k)+"_mc"].iniX;
this["foto"+String(k)+"_mc"].y = this["foto"+String(k)+"_mc"].iniY;}

for(i=1; i<7; i++){
  this["foto"+String(i)+"_mc"].addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
  this["foto"+String(i)+"_mc"].num = i;
  if(i <= 3){
    this["foto"+String(i)+"_mc"].iniX = (160*i)+(i-1)*100;
    this["foto"+String(i)+"_mc"].iniY = 100;
  }else if(i > 3){
    this["foto"+String(i)+"_mc"].iniX = (160*(i-3))+((i-3)-1)*100;;
    this["foto"+String(i)+"_mc"].iniY = 260;
  }
}

function startDragging(me:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
  MovieClip(me.currentTarget).startDrag(true);
  currentDragged = MovieClip(me.currentTarget);}

function stopDragging(evt:Event):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
  stopDrag();
  checkPosition(currentDragged);}

function firstCheck(mcR:MovieClip){
for(q = 0; q < icons.length; q++){
    if(this[icons[q]].y == 455){
        posX = Math.abs(mcR.x - this[icons[q]].x);
        if(posX < 10){
            returnToInitial(mcR.num);
        }
    }
}
}

function checkPosition(mc:MovieClip){
firstCheck(mc);
if(mc.y > 420 && mc.y < 490){
    mc.y = 455;
    for(k=0; k<6; k++){
        if(mc.x > 60+(k*135) && mc.x < 120+(k*135)){
            mc.x = 90+(k*135);
            array[k] = mc.num;
        }
    }
}
}

Solution

  • For making the movieclip you are dragging appear on top of everything else, add this to startDragging:

    setChildIndex(MovieClip(me.currentTarget), this.numChildren-1);