Hi there,
I used a tutorial to create a drag and drop facility on an application I am working on. This works fine, but I was wondering how I could extend this code to run a function or play a movie clip when all the movie clips are dragged to their correct places. I can’t figure out where in the code I should put this.
Any help would be appreciated, heres my drag and drop code:
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class BodyLanguageTTD extends Sprite
{
var xPos:int;//Stores the initial x position
var yPos:int;//Stores the initial y position
public function BodyLanguageTTD()
{
addEventListener(Event.ADDED_TO_STAGE, addedHandler);
addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
}
private function addedHandler(event:Event):void
{
this.x = stage.stageWidth / 2;
this.y = stage.stageHeight / 2;
addListeners(redSquare, whiteSquare, greenSquare);
}
private function removedHandler(event:Event):void
{
}
private function getPosition(target:Object):void
{
xPos = target.x;
yPos = target.y;
}
private function dragObject(e:MouseEvent):void
{
getPosition(e.target);
e.target.startDrag(true);
}
private function stopDragObject(e:MouseEvent):void
{
if (e.target.hitTestObject(getChildByName(e.target.name + "Target")))
{
e.target.x = getChildByName(e.target.name + "Target").x;
e.target.y = getChildByName(e.target.name + "Target").y;
trace("well done you got it right")
}
else
{
e.target.x = xPos;
e.target.y = yPos;
}
e.target.stopDrag();
}
private function addListeners(... objects):void
{
for (var i:int = 0; i < objects.length; i++)
{
objects*.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
objects*.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
}
}
}
}