2

I am trying to create a game for kids where they can drag letters on to a stage to make words.

I want to add a 'trash can' where users can drag letters they no longer need to dispose of them. I have created the movie clip but am totally unsure how to make it function using AS3.

I would also like to add a reset button so that the stage reverts to it's original state. Again, I have drawn it up and added the little as3 that I am aware of (to make it a button) but if anyone could assist with how to actually make this happen, I would be grateful.

The files are here: SWF | FLA and the code for the game is as follows:

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 && 
         e.currentTarget.name != "copy" )
    {
        var objectClass:Class = 
        getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;

        var copy:MovieClip = new objectClass();
        copy.name = "copy"; 
        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();
}


resetButton.addEventListener(MouseEvent.CLICK, reset);
resetButton.buttonMode = true;

function reset(event:MouseEvent):void
{

//Not sure what AS3 to add here to reset to original state

}
Danack
  • 24,939
  • 16
  • 90
  • 122
James
  • 65
  • 6

1 Answers1

2

I have already gave you the solution here Flash AS3 Clone, Drag and Drop

Here, I am providing a detail solution on how to drag objects inside a bin and remove them.

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.

First create trash-can MovieClip on the stage and give it an instance name 'trashCan' and add following lines to your onStop()(below e.currentTarget.stopDrag();)function like so:

UPDATE:

var copiedObjsArr:Array = [];    

function onStop(e)
{
    if ( e.target.dropTarget != null && 
     e.target.dropTarget.parent == dest && 
     e.currentTarget.name != "copy" )
    {
       //Code here remains same
       //.......

       //Keep collecting copied letters for further access in `reset()` function 
       copiedObjsArr.push(copy);
    }
    else if(e.currentTarget.name == "copy") //this is 'else if' (newly added)
    {
       var tarObject:MovieClip = e.currentTarget;

       // These detects collision of dragged object with the trashCan
       if(tarObject.hitTestObject(trashCan)) {

          //These removes dragged object from the display list (not from the memory)
          removeChild(tarObject); 

          tarObject = null; //to garbage
       }
    }

    e.currentTarget.stopDrag();
}

And your reset() becomes like so:

 function reset(event:MouseEvent):void
 {

     if(copiedObjsArr.length > 0)
     {
         //Traverse through all copied letters
         for(var i:int = 0; i<copiedObjsArr.length; i++)
         {
             var objToRemove:MovieClip = copiedObjsArr[i];

             removeChild(objToRemove);

             objToRemove = null;
         }

         //Finally empty the array
         copiedObjsArr = [];
     }
 }
Community
  • 1
  • 1
Rajneesh Gaikwad
  • 1,193
  • 2
  • 14
  • 30
  • Thanks again Rajneesh. I do not understand how to write actionscript well so that is why I posted a new question (I tried to implement the code you gave me last time but was unsuccessful). I have cut and paste the code you have provided and it works however the console gives me the message `"ReferenceError: Error #1074: Illegal write to read-only property currentTarget on flash.events.MouseEvent. at wordwork_fla::MainTimeline/onStop()".` Do you know if I have done something wrong? – James Feb 16 '14 at 04:29
  • Now it doesn't work at all - I get the error: `Scene 1, Layer 'Actions', Frame 1, Line 47 1105: Target of assignment must be a reference value.` – James Feb 16 '14 at 04:45
  • I have created `tarObject` as `currentTarget`. Try this change. – Rajneesh Gaikwad Feb 16 '14 at 05:15
  • Thank-you! Works great. Only one issue, that is if you try do drag one of the original letter movie clips (e.g. not a copy) to the trash can then the movie clip stays stuck with the cursor. Is there any way around this? – James Feb 16 '14 at 05:19
  • Put this new code inside `if` statement of `onStop()`; – Rajneesh Gaikwad Feb 16 '14 at 05:22
  • Sorry, I've cut and paste it to lie within the if statement of the `onStop` function but it renders the code invalid when I do this. – James Feb 16 '14 at 05:48
  • Do you want to make original letters also to move to trash? – Rajneesh Gaikwad Feb 16 '14 at 05:53
  • I want the original letter to remain in the starting position at all times. At the moment, if you drag letters straight to the trash can without first making a copy, they stick to the cursor. – James Feb 16 '14 at 06:01
  • I have updated the code. Also revert back the previous changes you made. – Rajneesh Gaikwad Feb 16 '14 at 06:12
  • Fantastic! Works now! Thanks so much. Sorry to keep bugging you, but any tips for the reset button? – James Feb 16 '14 at 06:25
  • See the update. I have added `Array` to collect your copied objects, see `if` statement. Also updated your `reset()` function. – Rajneesh Gaikwad Feb 16 '14 at 06:58
  • The reset works fine until a user drags a letter to the trash can. If you press the button after this, the message `ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at wordwork_fla::MainTimeline/reset()` appears. Any ideas why this might be? – James Feb 16 '14 at 07:21
  • Found the solution to the error #2025 in reset function.. Instead of `removeChild(objToRemove);` I changed to: `if (objToRemove.parent) { objToRemove.parent.removeChild(objToRemove); }` – James Feb 16 '14 at 10:51