3 Replies Latest reply on Jan 8, 2008 7:45 AM by niki tsanov

    Create Drag and Drop Movie Clips Dynamically

    yenniie
      I'm creating a game with an inventory system which updates and adds a movie clip as the user clicks on an item to add to it.

      If the user clicks on itemBtn, the user adds a new item (placeMC+itemNum) in the inventory system. Now the user must be able to drag the new item into its target destination. Does anyone know how to use drag and drop in a dynamically created movie clip? Or would you suggest any other alternative?

      Here's my code, it won't work:


      itemCtr = 0;

      itemBtn.onRelease = function(){
      newName = "placeMC"+itemCtr;
      newPos = placeMC._x + 120 * itemCtr;
      duplicateMovieClip(placeMC, newName ,itemCtr+1); //this.getNextHighestDepth()
      loadMovie("gamit/icon"+itemCtr+".jpg", [newName]);
      setProperty(newName, _x, newPos);
      set("placeMC"+itemCtr, newPos);
      itemCtr++;
      //track the item in position
      }
      this["placeMC"+itemCtr].onPress = function(){
      startDrag(this,true);
      }
      this["placeMC"+itemCtr].onRelease = function(){
      stop();
      if(this["placeMC"+itemCtr].hitTest(destinationMC) == true){
      setProperty(this,_visible, false);
      }
      }

      Any suggestions would be highly appreciated. Thank you!
        • 1. Re: Create Drag and Drop Movie Clips Dynamically
          niki tsanov
          So if you use attachMovie method is better because you have absolutly control into the new attachted movieclip just you make the movieclip who is stored into the library, make one button into the movieclip and put action button.onPress=function()
          {
          this._parent.startDrag(false);
          }
          The duplicateMovie method you can just put the code into the MovieClip who you duplicate
          on(release)
          {
          this.startDrag(false);
          }
          and when new duplicate new movieclip he has already code for drag but i think the attachMovie is better :)
          • 2. Re: Create Drag and Drop Movie Clips Dynamically
            yenniie Level 1
            thanks for the reply. I used attachedMovie now. I placed the dynamic movie clip in another separate movie clip. however, it only creates one other clip, and doesn't follow the position. I attached my code again. hope someone could help.

            itemBtn.onRelease = function(){
            newName = "placeMC"+itemCtr;
            newPos = _root.overlayMC.placeMC._x + 120 * itemCtr;
            _root.overlayMC.attachMovie("placeMC", newName, this.getNextHighestDepth());
            setProperty(newName, _x, newPos);
            setProperty(newName, _xscale, 50);
            setProperty(newName, _yscale, 50);
            _root.overlayMC[newName + itemCtr] = newPos;
            itemCtr++;
            trace(newPos);
            }
            • 3. Re: Create Drag and Drop Movie Clips Dynamically
              niki tsanov Level 1
              The problem is there the movie cant get the instance of your new movieclip your action is
              newName = "placeMC"+itemCtr;
              .....
              setProperty(newName, _x, newPos);

              the movie is try to get _root.newName and your new movieclip is attached into _root.overlayMC you can fixed when you change newName to newName = _root.overlayMC["placeMC"+itemCtr]; or difine new movieclip like this
              var mc:MovieClip = _root.overlayMC.attachMovie("placeMC", newName, _root.overlayMC.getNextHighestDepth());

              and then setProperty to the new deffined MovieClip MC and the this.getNextHighestDepth() get the highest depth from the button not from the _root.overlayMC :)