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;
}
e.target
withe.currentTarget
– null.point3r