# Shared Object for Drop Target

**URL:** <https://forum.kirupa.com/t/shared-object-for-drop-target/299918>\
**Category:** flash\
**Created:** [November 12, 2009, 4:35pm UTC](https://forum.kirupa.com/t/shared-object-for-drop-target/299918 "2009-11-12T16:35:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![raizor](https://avatars.discourse-cdn.com/v4/letter/r/8baadc/32.png) [@raizor](https://forum.kirupa.com/u/raizor)\
**Post date:** [November 12, 2009, 4:35pm UTC](https://forum.kirupa.com/t/shared-object-for-drop-target/299918/1 "2009-11-12T16:35:46Z")

</div>

I’ve got a drag and drop application that needs some attention…

I’d like the drop target array to be “remembered” with a shared object - this way, when the SWF starts up again, the objects are either on the drop targets where they left off or are where they were placed originally.

I’ve also attached the FLA for this and the code is below.

If anyone could help out that would be awesome!

import fl.transitions._;  
import fl.transitions.easing._;  
import flash.display._;  
import flash.events._;  
import draganddrop.\*;  
// set up variables  
// dropTargetNumber is one way of handling naming convention for the drop targets.  
var dropTargetNumber:int = 0;  
// create the drop targets  
createDropTarget(scale1,0,0,40,20);  
createDropTarget(scale2,0,0,40,20);  
createDropTarget(scale3,0,0,40,20);  
createDropTarget(scale4,0,0,40,20);  
// make the appropriate objects draggable  
makeDraggable(weight1);  
makeDraggable(weight2);  
makeDraggable(weight3);  
makeDraggable(weight4);  
// set up an array that determines the front-to-back positioning of the items contained within  
var layerArray:Array = new Array;  
layerArray.push(weight3);  
layerArray.push(weight2);  
layerArray.push(weight1);  
layerArray.push(weight4);  
layerArray.push(scaleReading1);  
layerArray.push(scaleReading2);  
layerArray.push(scaleReading3);  
layerArray.push(scaleReading4);  
// This function sets up a given MovieClip to make it draggable  
function makeDraggable(mc:MovieClip) {  
mc.prevX = mc.x;  
mc.prevY = mc.y;  
mc.buttonMode = true;  
mc.addEventListener(MouseEvent.MOUSE\_DOWN, clipPressHandler);  
mc.addEventListener(MouseEvent.MOUSE\_UP, clipReleaseHandler);  
}  
// This function creates a single drop target on top of the given MovieClip  
function createDropTarget(mc:MovieClip,extraWidth:int,extraHeight:int,offsetX:int,offsetY:int){  
var target:dragDropTarget = new dragDropTarget();  
target.createMCDropTarget(mc,extraWidth,extraHeight);  
dropTargetNumber ++;  
target.x = target.x + offsetX;  
target.y = target.y + offsetY;  
target.name = “target” + (dropTargetNumber);  
// trace ("Created dropbox: " + target.name);  
this.addChild(target);  
}  
// Handles the mouse click  
function clipPressHandler(e:MouseEvent):void {  
var mc;  
mc = e.target;  
addChild(mc);  
if (mc.assignedDropBox != null){  
mc.assignedDropBox.retrieveMC();  
mc.assignedDropBox.storedMC = null;  
}  
mc.startDrag(true);  
mc.buttonMode = true;  
mc.addEventListener(MouseEvent.MOUSE\_MOVE, clipMoveHandler);  
mc.addEventListener(MouseEvent.MOUSE\_DOWN, clipPressHandler);  
mc.addEventListener(MouseEvent.MOUSE\_UP, clipReleaseHandler);  
}  
// Handles the mouse release  
function clipReleaseHandler(e:MouseEvent):void {  
var mc:MovieClip = e.target as MovieClip;  
mc.stopDrag();  
[//mc.removeEventListener](https://mc.removeEventListener)(MouseEvent.MOUSE\_MOVE, clipMoveHandler);

checkForDropTarget(mc);  
}  
// Smoothes out the dragging of objects  
function clipMoveHandler(e:MouseEvent):void {  
e.updateAfterEvent();  
}  
// Handles the drop portion of the drag and drop  
function checkForDropTarget(pMC:MovieClip):void {  
// trace(pMC.dropTarget.name);  
if(pMC.dropTarget == null){  
pMC.x = pMC.prevX;  
pMC.y = pMC.prevY;  
return;  
}  
if(pMC.dropTarget.name.substr(0,6) == “target”) {  
if (pMC.dropTarget.isFull == true){  
// trace(“Drop target is full!”);  
pMC.x = pMC.prevX;  
pMC.y = pMC.prevY;  
return;  
}  
else{  
// trace("Dropped in: " + pMC.dropTarget.name);  
pMC.dropTarget.storeMC(pMC);  
pMC.x = pMC.dropTarget.x;  
pMC.y = pMC.dropTarget.y - 25;  
correctLayers(layerArray);  
}  
}  
else {  
// trace(“Dropped outside handler”);  
returnMC(pMC);

}  
}  
// Given an Array, use addChild() to put them in the appropriate front-to-back order  
function correctLayers(itemArray:Array){  
for (i=0;i\<itemArray.length; i++){  
this.addChild(itemArray\*);

}  
}  
// Handles the Tween of a given MovieClip to its original position  
function returnMC(mc:MovieClip){  
var xTween = new Tween(mc,“x”,Regular.easeOut,mc.x,mc.prevX,1,true);  
var yTween = new Tween(mc,“y”,Regular.easeOut,mc.y,mc.prevY,1,true);  
correctLayers(layerArray);  
}  
stop();

And, here’s the drag and drop class:

package draganddrop{  
import flash.display.\*;  
public class dragDropTarget extends MovieClip{

// This function creates a single drop target MovieClip  
public function dragDropTarget(){  
}  
public var graphicsMC:MovieClip;  
public var storedMC:MovieClip;  
public var isFull:Boolean;

public function createMCDropTarget(mc:MovieClip,extraWidth:int,extraHeight:int) {  
graphicsMC = mc;  
var g:Graphics = this.graphics;  
g.beginFill(0xFFFFFF, 1);  
g.lineStyle(1, 0, 1);  
g.drawRect((0-(mc.width+extraWidth)/2),(0-(mc.height+extraHeight)/2),(mc.width+extraWidth),(mc.height+extraHeight));  
g.endFill();  
this.x = mc.x;  
this.y = mc.y;  
// trace(“x=”+this.x+", y="+this.y+", width="+this.width+", height="+this.height);  
this.alpha = 0;  
// isFull = false;  
}  
public function createDropTarget(dropWidth:int,dropHeight:int) {  
var g:Graphics = this.graphics;  
g.beginFill(0xFFFFFF, 1);  
g.lineStyle(1, 0, 1);  
g.drawRect((0-dropWidth/2),(0-dropHeight/2),dropWidth,dropHeight);  
g.endFill();  
isFull = false;  
}  
public function storeMC(mc:MovieClip):void{  
// Puts a MovieClip inside the dropBox  
if (isFull){  
// trace(this.name + “.storedMC full!”);  
return;  
}  
else{  
// trace("stored " + mc.name + " to " + this.name + “.storedMC”);  
mc.assignedDropBox = this;  
// trace (mc.assignedDropBox);  
storedMC = mc;  
isFull = true;  
}

}  
public function retrieveMC(){  
// Returns the MovieClip stored in the dropBox  
if (isFull == false){  
// trace(this.name + “.storedMC empty!”);  
return;  
}  
else{  
// trace("retrieved " + storedMC.name + " from " + this.name + “.storedMC”);  
isFull = false;  
return storedMC;  
}  
}

}  
}
