A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: drag and drop one of many movieclips dynamically

  1. #1
    Member
    Join Date
    Sep 2005
    Posts
    64

    drag and drop one of many movieclips dynamically

    Hello all,

    I have a problem, as a newcomer to AS3. I'm looking to be able to pick up one of 36 movieclips on the stage, and drag it to a hotspot. If it lands on the hotspot, then it does something, otherwise it just gets dropped.

    How do I dynamically change the "target_mc" to be whichever movieclip I select?

    This code works fine, but that's because eventlistener is set-up explicitly for a given mc.

    Code:
    // sprites
    import caurina.transitions.*;
    
    var drag_mc:MovieClip = energy_sprites.play_wave;
    var target_mc:MovieClip = energy_sprites.hotspot_sea;
    
    drag_mc.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
    
    function beginDrag(e:MouseEvent):void {
    
    	stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMC);
    	stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
    }
    
    
    function endDrag(e:MouseEvent):void {
    	stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveMC);
    	
    	if (drag_mc.hitTestObject(target_mc)) {
    		Tweener.addTween(drag_mc,{x:mouseX,
    		  y:mouseY,
    		  time:1,
    		  transition:"easeIn"});
    		trace("HIT ME");
    
    	} else {
    		Tweener.addTween(drag_mc,{x:mouseX,
    		  y:mouseY,
    		  time:1,
    		  transition:"easeIn"});
    	}
    	stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
    }
    
    function moveMC(e:MouseEvent):void {
    
    	Tweener.addTween(drag_mc,{x:mouseX,y:mouseY,time:0.5,transition:"easeIn"});
    	e.updateAfterEvent();
    }

    Thanks a million for any thoughts on this.


  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    So you have many draggables other than play_wave and many hotspots other than hotspot_sea?

    Since your target_mc doesn't have anything special done to it, to change which instance is the target all you have to do is reassign target_mc to another instance.

    To allow other clips to be dragged, they will have to have the beginDrag listener added to them. Instead of setting drag_mc at the beginning, set it in beginDrag. And unset it in endDrag. Technically, you don't need drag_mc at all since you could just use the event target in all these functions. And moveMC is weird. I don't know why you want to tween on each mouse movement. But those are optional changes.


    Code:
    // sprites
    import caurina.transitions.*;
    
    var drag_mc:MovieClip = null; //initially unset
    var target_mc:MovieClip = energy_sprites.hotspot_sea;
    
    energy_sprites.play_wave.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
    energy_sprites.play_baseball.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
    //etc for your other sprites.  You could put them all in an array and do this in a loop.  Or you could set them mouseChildren = false and use a single listener on their container.
    
    function beginDrag(e:MouseEvent):void {
    	drag_mc = e.currentTarget;
    	stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMC);
    	stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
    }
    
    
    function endDrag(e:MouseEvent):void {
    	stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveMC);
    	
    	if (drag_mc.hitTestObject(target_mc)) {
    		Tweener.addTween(drag_mc,{x:mouseX,
    		  y:mouseY,
    		  time:1,
    		  transition:"easeIn"});
    		trace("HIT ME");
    
    	} else {
    		Tweener.addTween(drag_mc,{x:mouseX,
    		  y:mouseY,
    		  time:1,
    		  transition:"easeIn"});
    	}
    	stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
            drag_mc = null;
    }
    
    function moveMC(e:MouseEvent):void {
    	Tweener.addTween(drag_mc,{x:mouseX,y:mouseY,time:0.5,transition:"easeIn"});
    	e.updateAfterEvent();
    }

  3. #3
    Member
    Join Date
    Sep 2005
    Posts
    64
    Thanks indeed for the tip.

    I tried your suggestion, but I got this error. Have I missed something?

    1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:MovieClip.

    relating to this line

    Code:
    	drag_mc = e.currentTarget;

  4. #4
    Member
    Join Date
    Sep 2005
    Posts
    64
    drag_mc = e.currentTarget as MovieClip;

    8•)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center