James
James

Reputation: 65

Flash AS3 Clone, Drag & Drop

I am a teacher and with the help of some of you, I managed to create very basic word game for kids where they are able to create words from letters. SWF | FLA

I have, however, been unable to continue to drag and drop the letters once they have been cloned from their originals on the side of the stage. Is anyone able to advise how I might be able to add this functionality to my existing code?

import flash.display.MovieClip;

for (var i=1; i<27; 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();
}

I would also love to include a 'bin' where kids can drag letters if they do not wish to have them on stage any more. Any ideas for how I could add this would be greatly appreciated.

Many thanks.

Upvotes: 1

Views: 967

Answers (1)

Rajneesh Gaikwad
Rajneesh Gaikwad

Reputation: 1193

If you want similar behavior for dragged object (i.e after dragging, a copy is created)

add these line to your onStop(e) like so,

copy.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
copy.addEventListener(MouseEvent.MOUSE_UP, onStop);

But if you want only dragging and prevent a copy, then either create another function say dragCopiedObject() and apply the logic of dragging and not copying in that new function

or

identify between copied or original object in the same onStop(e) function and prevent from copying.

If you don't get it then let me know I will explain it to you in detail.

For dropping copied objects inside a bin, after dragging is stopped, check collision with bin object. for more info see,

copiedObject.hitTestObject(binObject)

For e.g.

if(copiedObject.hitTestObject(binObject)) {

     removeChild(copiedObject);
}

UPDATE: To identify between copied objects use name property like so:

copy.name = "copy";

I have modified your code,

function onStop(e)
{
    if ( e.target.dropTarget != null && 
         e.target.dropTarget.parent == dest && 
         e.currentTarget.name != "copy"         ) //This is newly added
    {
        var objectClass:Class = 
        getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;

        var copy:MovieClip = new objectClass();
        copy.name = "copy"; //This is newly added
        this.addChild(copy);
        copy.x = e.currentTarget.x;
        copy.y = e.currentTarget.y;

        e.currentTarget.x = sx;
        e.currentTarget.y = sy;

        copy.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
        copy.addEventListener(MouseEvent.MOUSE_UP, onStop);
    }    

    e.currentTarget.stopDrag();
}

Upvotes: 1

Related Questions