A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25

Thread: How to add duplicate movieclip "box_mc" in attached code?

  1. #21
    Junior Member
    Join Date
    Mar 2011
    Posts
    26
    OK, I tried what you suggested and the shapes drag and duplicate and if not dragged on top of their target the shapes dissappear. But, the shapes don't snap to their target at the bottom of the stage if the shapes are dragged on top of the target. Here is the function "dropItem" that I have now:

    Actionscript Code:
    function dropItem(event:MouseEvent):void {
      var myTarget:Sprite = Sprite(event.target);
      myTarget.stopDrag();
      //I don't know about this logic.  It's equivalent to yours, but it doesn't look right.
      if (myTarget.dropTarget != null && myTarget.dropTarget.parent == myTarget){
         myTarget.removeEventListener(MouseEvent.MOUSE_UP, dropItem);
      } else {
       myTarget.parent.removeChild(myTarget);
      }
    }

  2. #22
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't quite get how your targets and the draggables are related. I don't understand how you can drag something on top of its own child (wouldn't the child move?)

    Whatever. See the bolded line below for where to add your positioning code. You may have to use localToGlobal and globalToLocal to translate between coordinate spaces.

    Code:
    function dropItem(event:MouseEvent):void {
      var myTarget:Sprite = Sprite(event.target);
      myTarget.stopDrag();
      //I don't know about this logic.  It's equivalent to yours, but it doesn't look right.
      if (myTarget.dropTarget != null && myTarget.dropTarget.parent == myTarget){
         myTarget.removeEventListener(MouseEvent.MOUSE_UP, dropItem);
         //put positioning code here.
      } else {
       myTarget.parent.removeChild(myTarget);
      }
    }

  3. #23
    Junior Member
    Join Date
    Mar 2011
    Posts
    26
    I have added comments to the original function "dropItem", maybe this will help understand the original code:

    Actionscript Code:
    function dropItem(event:MouseEvent):void {
        event.target.stopDrag();
        //below creates a text string from the word “target” plus the name of the shape dropped,
        //creating a name that matches a target, such as "targetsquare_mc"
        var myTargetName:String = "target" + event.target.name;
        //this line looks through the document for an object with that name, and saves a
        //reference to it in a variable called "myTarget"
        var myTarget:DisplayObject = getChildByName(myTargetName);
        //The if statement checks to make sure that the dropTarget, or the item that is under
        //the shape when the mouse button is released, is not null, or empty. In other words, if
        //we release square_mc over the movie clip named “targetsquare_mc“, we receive a positive
        //reply. If the peg is released anywhere else, we will receive a negative reply.
        if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
            //When a shape is placed over the correct target, the if statement returns true,
            //and that shape will be locked and no longer dragable.
            event.target.removeEventListener(MouseEvent.MOUSE_DOWN, duplicateItem);
            event.target.removeEventListener(MouseEvent.MOUSE_UP, dropItem);
            event.target.buttonMode = false;
            //once a shape is placed, it fits perfectly over the target shape
            event.target.x = myTarget.x;
            event.target.y = myTarget.y;
        } else {
            //this moved the shapes back to their origin, but I like your idea of just
            //removing them as their are duplicates now
            event.target.x = startX;
            event.target.y = startY;
        }

  4. #24
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Ohhh. You are using a naming convention to associate your draggables and targets. That's what I missed before. So in my code myTarget was the dragged item (because it was the event target), in yours it was the intended dropTarget.

    You can put that back in. But I'd prefer to associate them without using the name property. You could simply set a property on the clip, or you could use a Dictionary to use one as a key and the other as a value.

  5. #25
    Junior Member
    Join Date
    Mar 2011
    Posts
    26
    Woah! I think my head hurts. I give up... so close but yet so far...

    Thank you for your help, I really do appreciate it.

    Quote Originally Posted by 5TonsOfFlax View Post
    Ohhh. You are using a naming convention to associate your draggables and targets. That's what I missed before. So in my code myTarget was the dragged item (because it was the event target), in yours it was the intended dropTarget.

    You can put that back in. But I'd prefer to associate them without using the name property. You could simply set a property on the clip, or you could use a Dictionary to use one as a key and the other as a value.

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