AS: Main, it's so leet that even the guys from the 1337 IRC channel frequent!
Hello, first of all let me start by thanking spamburger for the original tutorial.
This will be more in depth and will cover 3 things:
1)Dragging using ClipEvents
2)Dragging using startDrag and stopDrag
3)Most Common Use
and no, no trig toast, and saying that ain't making you look clever :P
Dragging using Clip Events
Draggin using clip events is pretty easy, all you have to do is add
this._x=_root._xmouse;
this._y=_root._ymouse;
this will auto set the correct _x and _y positions for the movieclip, so your code is actually
onClipEvent(enterFrame){
this._x=_root._xmouse;
this._y=_root._ymouse;
}
you can also have control over that with 'if' conditions and variables , for example you can only "drag" if a variable is true using
onClipEvent(enterFrame){
if(var1==true){
this._x=_root._xmouse;
this._y=_root._ymouse;
}
}
and then setting that varialbe to true or false using on(press) and on(release) (which are explained well in my replay button tutorial .
Dragging using flash's built in startDrag and stopDrag
There are several variations to startDrag and stopDrag, i will only cover the ones in the movieclip.startDrag format and not the ones in the startDrag(movieclip format, though practically they are identical.
We will focus on two commands
startDrag - startDrag([lock:Boolean, left:Number, top:Number, right:Number, bottom:Number]) : Void
don't be afraid of the spooky full syntax :P you sill ususally just use
movieClip.startDrag
and not any of the spooky looking stuff :P
the parameters are pretty simple though:
Parameters
lock A Boolean value specifying whether the draggable movie clip is locked to the center of the mouse position (true) or locked to the point where the user first clicked the movie clip (false). This parameter is optional.
left, top, right, bottom Values relative to the coordinates of the movie clip’s parent that specify a constraint rectangle for the movie clip. These parameters are optional.
none of the function's parameters are needed, you can just go
movieClip.startDrag();
and it will work perfectly fine, the parameters are very useful though,
movieClip.startDrag(true);
will cause the movieclip to autosnap to the mouse rather then just get dragged from wherever it is
the top,down,left,right parameters allow you to specify the area it can be dragged in, extremely useful for several things:
1)you can pass the same top and buttom creating an horizontal scroller
2)you can pass the same left and right creating a vertical scroller
3you can just limit the position of the drag to create bounderies, useful in many cases.
so basically you can do
movieClip.startDrag(0,0,550,400);
to make it drag only in the stage, play with it, it's useful
you can also combine the lock and the position with
movieClip.startDrag(true,0,0,550,400);
very useful indeed
Most Common Use
on(press){
this.startDrag(true);
}
on(release){
this.stopDrag();
}
this will create a simple drag.
Ask any questions here :)