Drag and drop objects

A simple movie: Create a new movie clip and use these scripts

  • on(press) {startDrag(this);}
  • on(release) {stopDrag();}

Notice that it only works if you click on the egg and drag, not other places on the stage. Also note that it is possible to completely drag the egg off the stage.

Limiting movement :

  • startDrag(this,true,0,0,250,200);
  • drag this object
    • true = show the center point when the mouse moves
    • 0,0 = top left corner you cannot drag past
    • 250,200 = size of stage = bottom right corner you cannot drag past

Moving to a target

If the egg is placed on the crate it will stay there in the crate. Otherwise it will spring back to the top corner of the stage.

The hitTest action returns true if any part of an object is within the boundaries of another object. (NOTE: the exclamation point, !, means not).

The code is attached to the egg and says that when you release the egg it will stop following the mouse movement (stopDrag) and if it is NOT in the crate (which is on the main stage or "root" of the movie) then it will return to position 10,20.

      if (! this.hitTest(_root.crate){
      this._x = 10;
      this._y = 20; }}