Question

I have an array of 8 movie clips which can be dragged and dropped onto an MC that is their common hitObject.

I would like whichever mc is being dragged to be added as the child of the hitObject MC when it is dropped, however I am having troubles setting up the code. Currently, only one specific instance will be added as a child of the hitObject because I don't know what to write inside the addChild() parameter aside from a specific instance name (none of the following are acceptable: e.target, the array name, the MovieClip name).

Here's my code --any and all help will be most appreciated:

import flash.events.MouseEvent;
import flash.display.MovieClip;

var redArray:Array = [red,red1,red2,red3,red4,red5,red6,red7];

redArray.forEach(setupDrag);
function setupDrag(dragger:MovieClip, index:int, array:Array):void {
    dragger.addEventListener(MouseEvent.MOUSE_DOWN, dragRed);
    dragger.buttonMode=true;}

redArray.forEach(setupDrop);
function setupDrop(dropper:MovieClip, index:int, array:Array):void {
    dropper.addEventListener(MouseEvent.MOUSE_UP, dropRed);
    dropper.buttonMode=true;}

var dirt:MovieClip

function dragRed(e:Event):void{
        dirt = e.currentTarget as MovieClip;
        e.target.startDrag();
}

function dropRed(e:Event):void{
        e.target.stopDrag();
        if (e.target.hitTestObject(drawer_mc))
        {
        drawer_mc.addChild(red1);
        red1.y=10;
        }
}

Thanks in advance!

Was it helpful?

Solution

You're almost there. You want to pass either target or currentTarget as the argument to addChild. I think the following should do it (I've just posted the bits I've updated).

And there's a good explanation of the difference between target and currentTarget here.

var dirt:MovieClip;

function dragRed(e:Event):void {
        // sounds like currentTarget and target will both work in your
        // case, but stick to one or the other for consistency
        dirt = e.currentTarget as MovieClip;
        dirt.startDrag();
}

function dropRed(e:Event):void{
        // assigning the currentTarget to your dirt variable means you don't 
        // keep needing to refer to e.currentTarget throughout the function
        dirt = e.currentTarget as MovieClip;
        dirt.stopDrag();
        if (dirt.hitTestObject(drawer_mc)) {
            // now you're always adding the instance that triggered 
            // the mouse up event
            drawer_mc.addChild(dirt);
            dirt.y=10;
        }
}

OTHER TIPS

You're already accessing the dropped object via the event target property. Cast it as a DisplayObject or MovieClip, then pass it in the addChild() method:

function dropRed(e:Event):void
{
  var droppedObject:MovieClip = e.currentTarget as MovieClip;
  droppedObject.stopDrag();

  if(droppedObject.hitTestObject(drawer_mc))
  {
    drawer_mc.addChild(droppedObject);
    droppedObject.y=10;
  }
}
  • e.target refers to the clicked object.
  • e.currentTarget refers to the object addEventListener() was applied to.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top