Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

beginDrag.as

Name: beginDrag [MX] startDrag alternative - smooth dragging and multiple objects can be dragged at once
Author: senocular: www.senocular.com
Date: 1899-12-31T00:52:16.200
Documentation:
MovieClip BEGINDRAG (and ENDDRAG): alternative to
startDrag and stopDrag.  Function virtually the same way
in its use with some minor differences.  The advantage here
being that you can drag more than one item at a time easily
and smoothly.

Differences:
- any movieclip can be dragged at any time, even when another movieclip is already
being dragged
- because multiple movieclips can be dragged, endDrag must be used with a movieclip
reference to properly work (whereas stopDrag worked without one).
- updateAfterEvent is used on the clip's onMouseMove to allow for smooth dragging
- since onMouseMove is used to handle the drag, you cannot delete onMouseMove
while dragging the clip.  You CAN assign an onMouseMove event or have one prior to
beginDrag which will remain in effect, but deleting the onMouseMove will disrupt
this prototype.  If you need to delete your own mouseMove, use mc.onMouseMove = null;

target.beginDrag([lock [, left, top, right, bottom]]);
beginDrag(target [, lock [, left, top, right, bottom]]);

Arguments:
- target: (optional) the movieclip to be dragged.  This is not needed in the form
myMovieClip.beginDrag();  Can be either a movieclip reference or a string target.
- lock: (boolean, optional) determines whether the mouse is locked to the center of 
the clip when dragging.
- left , top , right, bottom: (1,t,r,b) (optional) if used, sets a constraint rectangle
for the movie clip (based on its center).

Returns:
- nothing

target.endDrag();
endDrag(target);

Arguments:
- target: the movieclip to stop dragging.  This is not needed in the form
target.beginDrag();  Can be either a movieclip reference or a string target.

Returns:
- nothing

Example:
// on a movieclip: press to drag
on(press){
	this.beginDrag(true,0,0,300,300); // constrained to 300x300 square
}
on(release, releaseOutside){
	this.endDrag();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
MovieClip.prototype.beginDrag = function(target, lock, l, t, r, b){
	if (typeof target == "string"){
		target = eval(target);
		if (!target) return trace("Warning: Invalid MovieClip for beginDrag. "+target);
	}else if (!(target instanceof MovieClip)){
		b=r; r=t; t=l; l=lock; lock=target; target=this;
	}
	if (target.$dragMethod) target.endDrag();
	target.$dragMethod = {MM:target.onMouseMove};
	ASSetPropFlags(target,"$dragMethod",1,1);
	target.addProperty("onMouseMove",arguments.callee.getMM,arguments.callee.setMM);
	ASSetPropFlags(target,"onMouseMove",3);
	var constrain = (arguments.length > 1);
	var off_x = 0, off_y = 0;
	if (!lock){
		off_x = target._parent._xmouse-target._x;
		off_y = target._parent._ymouse-target._y;
	}
	target.$dragMethod.drag = function(){
		target._x = target._parent._xmouse-off_x;
		target._y = target._parent._ymouse-off_y;
		if (constrain){
			if (typeof l == "object"){
				t = l.ymin;
				r = l.xmax;
				b = l.ymax;
				l = l.xmin;
			}
			if (target._x < l) target._x = l;
			else if (target._x > r) target._x = r;
			if (target._y < t) target._y = t;
			else if (target._y > b) target._y = b;
		}
		updateAfterEvent();
	}
}
MovieClip.prototype.beginDrag.getMM = function(){
	this.$dragMethod.drag();
	return this.$dragMethod.MM;
}
MovieClip.prototype.beginDrag.setMM = function(f){
	this.$dragMethod.MM = f;
}				  
MovieClip.prototype.endDrag = function(target){
	if (arguments.length){
		if (typeof target == "string") target = eval(target);
		if (!target) return trace("Warning: Invalid MovieClip for beginDrag. "+target);
	}else target = this;
	ASSetPropFlags(target,"onMouseMove",0,3);
	delete target.onMouseMove;
	if (target.$dragMethod.MM) target.onMouseMove = target.$dragMethod.MM;
	delete target.$dragMethod;
	target.startDrag(); // for _droptarget
	target.stopDrag();
}