1
votes

I have a problem with my Movie Clips switching positions when I drop them one atop the other. What should happen is that the Movie Clip should return to its original position unless I drop in a specific place which I have assigned. It works okay when I drop it at a blank spot or at the designated drop spot, only when I drop it on another Movie Clip do those two Movie Clips switch positions and they get messed up.

I've tried disabling everything with "mouseEnabled = false" and setting it back to "mouseEnabled = true" when I hover on any of the Movie Clips but it doesn't respond. Anyone have a clue?

function Placed(e){
    if(e.target.hitTestObject(Destination)) {
        e.target.stopDrag();
        e.target.enabled = false;
        e.target.mouseEnabled = false;      
    }

    else {
        e.target.x = xPos;
        e.target.y = yPos;
        e.target.stopDrag();
    }
}

This is the code that supposedly makes the MCs return to their original positions or stick to the designated spot. I think its crystal clear what the MCs should do given the code so I don't know why they decide to switch places when I place them atop each other.

EDIT*: I'll post my entire code below this sentence, it may help understand better what could be wrong with it. This code already has Vesper's and null.point3r's ideas but still hasn't solved the problem.

When I place the MC in the Destination, it will leave a clone of itself and disable the dragged MC. When I don't place it in the Destination, then no clone is left. I have 3 MCs created in order of MC1, MC2, MC3. When I put the older MC atop the newer ones, they switch places, say I place MC1 atop MC2 or MC3, but when I do the opposite, say MC3 atop MC2 or MC1, nothing happens, which should have been the case for the former as well.

import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.Event;

var xPos:int;
var yPos:int;
var temp = 0;
var Drag:DisplayObject;

//Count for Amount MCs
for (var i=1; i<4; i++)
{
    this["Amount" + i].addEventListener(MouseEvent.MOUSE_DOWN, CheckDrag);
    this["Amount" + i].addEventListener(MouseEvent.MOUSE_UP, Placed);
    this["Amount" + i].buttonMode = true;
}

//Gets original position of selected Amount and allows dragging
function Trace(e){
    getPosition(e.target);
    e.target.startDrag();
}

//Stores original position of selected/dragged Amount
function getPosition(target:Object):void{
    xPos = target.x;
    yPos = target.y;
}

//Check if a MC is being dragged
function CheckDrag(e){
    if(Drag != null) return;
    Drag = e.target;
    Trace(e);
}

//Checks if placed in Ledger, returns to original position if false
function Placed(e){
    if(Drag == null) return;

    //Add Child on Mouse_UP
    var Xtra:Class = getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;
    var Extra:MovieClip = new Xtra();
    this.addChildAt(Extra,1);
    Extra.x = xPos;
    Extra.y = yPos;

    //Disable Drag of selected Amount and set position in Desitnation
    if(e.target.hitTestObject(Destination)){
            e.currentTarget.stopDrag();
            e.currentTarget.enabled = false;
            e.currentTarget.mouseEnabled = false;       
    }

    //Return Amount to original position and remove Child
    else{
        e.currentTarget.x = xPos;
        e.currentTarget.y = yPos;
        e.currentTarget.stopDrag();
        removeChild(Extra);
    }
    Drag = null;
}
1
replace e.target with e.currentTargetnull.point3r
they still change places...IceCue

1 Answers

0
votes

You are apparently lacking flag indication, which makes your second MC to get assigned wrong coordinates. You need to have a variable that'll contain the info of whether there's a MC you're now dragging, so that if there is, your MCs' start drag code won't interfere. You have xPos and yPos assigned somewhere, and your code is doing like this:

function someListener(e:MouseEvent):void {
    xPos=e.target.x;
    yPos=e.target.y;
    e.target.startDrag();
}

You have to first check if there's an object being dragged, like this:

var draggedObject:DisplayObject;
function someListener(e:MouseEvent):void {
    if (draggedObject!=null) return; 
    // ^ this means something is being dragged, this clip shouldn't react
    draggedObject=e.target;
    xPos=e.target.x;
    yPos=e.target.y;
    e.target.startDrag();
}

Then, once you are about to stop dragging, you clear the draggedObject variable.

function Placed(e){
    if (draggedObject==null) return; // safety precaution
    if(e.target.hitTestObject(Destination)) {
        e.target.stopDrag();
        e.target.enabled = false;
        e.target.mouseEnabled = false;      
    }
    else {
        e.target.x = xPos;
        e.target.y = yPos;
        e.target.stopDrag();
    }
    draggedObject=null;
}

PS: you might want to avoid using manager class for this, and instead store those xPos and yPos on the MC that you're moving.