AS: Drag & Drop 2005-07-26 17:30:44
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 will auto set the correct _x and _y positions for the movieclip, so your code is actually
you can also have control over that with 'if' conditions and variables , for example you can only "drag" if a variable is true using
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
and not any of the spooky looking stuff :P
the parameters are pretty simple though:
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
and it will work perfectly fine, the parameters are very useful though,
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
to make it drag only in the stage, play with it, it's useful
you can also combine the lock and the position with
very useful indeed
Most Common Use
this will create a simple drag.
Ask any questions here :)