In flash, only the movieclip (sprite) of a video can call startdrag and enddrag. The simplest way to create an object is to call these two methods.
Myobj. addeventlistener (mouseevent. mouse_down, pickup); myobj. addeventlistener (mouseevent. mouse_up, place); function pickup (E: mouseevent): void {// trace ("press the mouse" );e.tar get. startdrag ();} function place (E: mouseevent): void {// trace ("move the mouse up" using cmde.tar get. stopdrag ();}
Myobj is a casual movieclip instance on the stage.
Startdarg can also set the drag boundary when dragging:
var ball: ball = new ball (30, math. random () * 0 xffffff); var posx: Number = stage. stagewidth/2; var posy: Number = stage. stageheight/2; var rectsize: Number = 200; ball. X = posx; ball. y = posy; addchild (ball); ball. addeventlistener (mouseevent. mouse_over, function () {mouse. cursor = mousecursor. hand}); ball. addeventlistener (mouseevent. mouse_out, function () {mouse. cursor = mousecursor. auto}); ball. addeventlistener (mouseevent. mouse_down, mousedownhandler); function mousedownhandler (E: mouseevent) {stage. addeventlistener (mouseevent. mouse_up, mouseuphandler); // if the first parameter is true, it indicates that the point where the mouse is located is automatically aligned to the center of the object-the so-called lock center ball. startdrag (true, new rectangle (posx-rectsize/2, Posy-rectsize/2, rectsize, rectsize); // draw a boundary for more intuitive display of graphics. clear (); graphics. linestyle (1); graphics. drawrect (posx-rectsize/2, Posy-rectsize/2, rectsize, rectsize);} function mouseuphandler (E: mouseevent) {ball. stopdrag () ;}
The following is a more complex example:
R1.addeventlistener (mouseevent. mouse_down, mousedownhandler); r1.addeventlistener (mouseevent. mouse_up, mouseuphandler); r2.addeventlistener (mouseevent. mouse_down, mousedownhandler); r2.addeventlistener (mouseevent. mouse_up, mouseuphandler); r3.addeventlistener (mouseevent. mouse_down, mousedownhandler); r3.addeventlistener (mouseevent. mouse_up, mouseuphandler); // trace (numchildren); // The total number of elements on the current stage VaR _ originpoint: Point = new point (); // function mousedownhandler (E: mouseevent) {// trace (E); // configure get, numChildren-1); // you can specify the zindex of the dragged object, otherwise, it may be blocked by other components during the drag process. // The Shadow VaR _ shadow: dropshadowfilter = new dropshadowfilter (); _ shadow is displayed during the drag process. distance = 10; _ shadow. alpha = 0.5; _ shadow. color = 0xffffff0000e.tar get. filters = [_ shadow]; mouse. cursor = mousecursor. hand;} // function mouseuphandler (E: mouseevent) when dragging is complete e.target.stopdrag(~~e.tar get. filters = NULL; If (e.tar get. droptarget is shape%e.target.droptarget.transform.colortransform%e.tar get. transform. colortransform; // set the color of the target object to be consistent with that of the source object} // restore the destination location to e.tar get. X = _originpoint.x;e.tar get. y = _ originpoint. y; mouse. cursor = mousecursor. arrow ;}
Source File Download: http://cid-2959920b8267aaca.skydrive.live.com/self.aspx/Flash/DragObject.rar