views:

75

answers:

2

Here is a working script to drag drop a ball from the stage into another target mc:

ball.addEventListener(MouseEvent.MOUSE_DOWN, pickUp); 
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);     
function pickUp(event:MouseEvent):void{ 
    var ballPoint:Point = ball.parent.localToGlobal( new Point(ball.x, ball.y) ); 
    ball.parent.removeChild(ball); 
    addChild(ball); 
    ball.x = ballPoint.x; 
    ball.y = ballPoint.y; 
    ball.startDrag(); 
}      
function dropIt(event:MouseEvent):void{ 
    ball.stopDrag(); 
    if(!event.target.dropTarget) { return };      
    var dropT:MovieClip = event.target.dropTarget.parent; 
    var ballPoint:Point = dropT.globalToLocal( new Point(ball.x, ball.y) );      
    ball.parent.removeChild(ball); 
    dropT.addChild(ball); 
    ball.x = ballPoint.x; 
    ball.y = ballPoint.y; 
}

It works well but I'm new to AS3 and I am having difficulty amending this script to drag the ball out of a parent mc (instead of the stage) and onto another mc (the target mc). To explain: imagine a ball, a box, a tri, + more shapes all within a scrolling mc (initial parent), the user scrolls to select a shape, then drag-drops onto a target mc on the stage.

The scrolling is no problem, but how would you amend the script above so that the ball can start inside a parent mc rather than on the stage itself?

Any suggestions welcome.

A: 

Couple of things. You don't need to do a ball.parent.remove(ball) call - the next addChild(ball) automagically removes the MC from where ever it is.

In the pickUp() call, do a this.stage.addChild( ball ), to put the ball into (on top of) everything else.

Then in the dropIt() call, do an addChild( ball ) on whichever MC you want to put the ball into...

Jos
Thanks for your help and I've experimented with your suggestions, but none of those ideas address my actual question which concerns dragging the ball *out of a parent mc* rather than *directly* on the stage itself.
ss
A: 

I've come up with a solution that works well. Along the way I also solved a common problem with strict mode and casting (see line 5 & 16 in the code below).

scroller.ball.addEventListener(MouseEvent.MOUSE_DOWN, pickUp); 
scroller.ball.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void  
{ 
   var dragIt:MovieClip = MovieClip(event.target);
   var dragPoint:Point = dragIt.parent.localToGlobal( new Point(dragIt.x, dragIt.y) ); 
   dragIt.parent.removeChild(dragIt); 
   stage.addChild(dragIt); 
   dragIt.x = dragPoint.x; 
   dragIt.y = dragPoint.y; 
   dragIt.startDrag();
}
function dropIt(event:MouseEvent):void  
{ 
   stopDrag();
   var dragIt:MovieClip = MovieClip(event.target);
   if(!dragIt.dropTarget) { 
      trace("you missed");
      return ;
   }; 
   var dropT:MovieClip = event.target.dropTarget.parent; 
   var dropPoint:Point = dropT.globalToLocal( new Point(dragIt.x, dragIt.y) ); 
   dragIt.parent.removeChild(dragIt); 
   dropT.addChild(dragIt); 
   dragIt.x = dropPoint.x; 
   dragIt.y = dropPoint.y;
}
ss