James
James

Reputation: 65

Drag, Drop and Clone - Flash AS3

I am a teacher trying to make a simple Flash AS3 game where students can drag any letter from a-z(movieclips) out to the middle of the stage to create words.

I am a total amateur with Flash and so using code snippets, I have been successful in allowing users to drag and drop letters but what I would really like is for users to be able to drag and drop a letter leaving the original letter movieclip in place and clone a new one as many times as is necessary.

Is anyone able to help me with the AS3 I would need to achieve this? Many thanks.

Upvotes: 2

Views: 1109

Answers (2)

loxxy
loxxy

Reputation: 13151

Here is a quick sample. FLA | SWF

CODE:

import flash.display.MovieClip;

for (var i=1; i<5; i++)
{
    this["object" + i].addEventListener(MouseEvent.MOUSE_DOWN, onStart);
    this["object" + i].addEventListener(MouseEvent.MOUSE_UP, onStop);
}    

var sx = 0,sy = 0;

function onStart(e)
{
    sx = e.currentTarget.x;
    sy = e.currentTarget.y;
    e.currentTarget.startDrag();
}

function onStop(e)
{
    if (e.target.dropTarget != null && 
    e.target.dropTarget.parent == dest)
    {
        var objectClass:Class = 
        getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;
        var copy:MovieClip = new objectClass();
        this.addChild(copy);
        copy.x = e.currentTarget.x;
        copy.y = e.currentTarget.y;
    }

    e.currentTarget.x = sx;
    e.currentTarget.y = sy;
    e.currentTarget.stopDrag();
}

Hoping you could take it ahead & turn into something useful for your kids.

Upvotes: 1

Bora Kasap
Bora Kasap

Reputation: 390

first, you can use a property for your letters called like "type" and "letter" for assuming is it original or a copy and which letter is it.

then initialize an empty object variable to use when clicked

var draggingObj:Object;

then you can check if user clicking on orginal letter to make a copy and move or clicking a copy just to move

if (Letter(clickedLetter).type == "original")
{
draggingObj = new Letter();
draggingObj.type = "copy";
draggingObj.letter = Letter(clickedLetter).letter;
}
else draggingObj = Letter(clickedLetter);

draggingObj.x = mousex;
draggingObj.y = mousey;

if you cannot implement this codeflow to your codeflow, you can write your own code here so we can help you properly

Upvotes: 0

Related Questions