How to DragAndDrop a scripted object

Hi everyone, I’m new to this site and flash altogether, please be gentle :stuck_out_tongue:

Ok, so as the title says I want to learn how to drag and drop an object that is created through script.


stop();
 
 
var facePlaced:Object;
var sOpenedMenu:String = "faces";
var sOpeningMenu:String = "faces";
 
//Set Mouse DragAndDrop
import flash.events.MouseEvent;
var offsetX:Number;
var offsetY:Number;
 
function startDragging(event:MouseEvent):void
{
 
    offsetX = event.stageX - facePlaced.x;
    offsetY = event.stageY - facePlaced.y;
 
 
    stage.addEventListener(MouseEvent.MOUSE_MOVE, dragFace);
}
 
function stopDragging(event:MouseEvent):void
{
 
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragFace);
}
 
function dragFace(event:MouseEvent):void
{
 
    facePlaced.x = event.stageX - offsetX;
    facePlaced.y = event.stageY - offsetY;
 
    event.updateAfterEvent();
}
facePlaced.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
facePlaced.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
// End of Drag and Drop
 
var spWorldHolder:Sprite=new Sprite();
this.addChild(spWorldHolder);
var spFaceHolder:Sprite=new Sprite();
spWorldHolder.addChild(spFaceHolder);
function onClickFace(event:MouseEvent):void {
 spWorldHolder.removeChildAt(0);
 var sFaceTempHolderName = event.target.name;
 var FaceTempHolder = sFaceTempHolderName.substr(0,7);
 var ClassReference:Class = getDefinitionByName(FaceTempHolder) as Class;
 facePlaced = new ClassReference();
 PlaceTheFace(facePlaced); 
}
function PlaceTheFace(facePlaced):void {
 facePlaced.x = 435;
 facePlaced.y = 150;
 spWorldHolder.addChildAt(facePlaced,0);
}
function openFacesMenu(event:MouseEvent):void {
 gotoAndPlay(6);
}
function openMouthsMenu(event:MouseEvent):void {
 sOpeningMenu = "mouths";
 if (sOpenedMenu == "faces") {
  gotoAndPlay(6);
 }
}
Face001btn.buttonMode = true;
Face001btn.addEventListener(MouseEvent.MOUSE_UP,onClickFace);
Face002btn.buttonMode = true;
Face002btn.addEventListener(MouseEvent.MOUSE_UP,onClickFace);
Face003btn.buttonMode = true;
Face003btn.addEventListener(MouseEvent.MOUSE_UP,onClickFace);
Face004btn.buttonMode = true;
Face004btn.addEventListener(MouseEvent.MOUSE_UP,onClickFace);
Face005btn.buttonMode = true;
Face005btn.addEventListener(MouseEvent.MOUSE_UP,onClickFace);
FacesMenuBtn.buttonMode = true;
FacesMenuBtn.addEventListener(MouseEvent.MOUSE_UP,openFacesMenu);
MouthsMenuBtn.buttonMode = true;
MouthsMenuBtn.addEventListener(MouseEvent.MOUSE_UP,openMouthsMenu);

Basicly what my code does is create an object (image of a face) at the center of the screen when a button is clicked. That object is created through script called “facePlaced” and I need to be able to drag and drop that object after it’s created.

The object I want to move is created when clicking on Face001btn (face002, and so on) by the function “onClickFace”

I have tested my drag and drop code with already placed instances and works fine, but I cant figure out how to assign that code to the newly created instance.

Any help with code examples would be greatly appreciated.

[URL=“http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Sprite.html#startDrag()”]Use the start and stop drag methods of the sprite class

You didn’t even read my post :frowning:

Like I said, the code above WORKS, the problem is assinging that code to an object created through script. I’ts like the function is not passing.

I was confused by what you were trying to do because you didn’t explain where the code was. You put the code directly into the movie clip right? Don’t do that, use the document class and classes. I think they might be why it isn’t working. Anyways, you should still use the Sprite’s implementation of Drag and Drop.