Be a Supporter!

AS: Drag & Drop

  • 7,438 Views
  • 56 Replies
New Topic Respond to this Topic
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
AS: Drag & Drop 2005-07-26 17:30:44 Reply

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 :)

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:29:03 Reply

Might as well post teh smooth drag here as well for reference.

onClipEvent(mouseMove){
this._x = _root._xmouse;
this._y = _root._ymouse;
updateAfterEvent();
}

= teh smooth
apply it to custom cursors, draggable clips etc.

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:41:06 Reply

At 9/14/05 01:29 PM, T-H wrote: = teh smooth
apply it to custom cursors, draggable clips etc.

it's not that recommanded, it'll cause serious lag with a lot of objects on the screen

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:45:44 Reply

At 9/14/05 01:20 PM, T-H wrote: LOL NO

The stage doesnt update after the drag, so IT ISNT SMOOTH!!!!!

NOT BETTER AARARAGGH
(Pwn).
At 9/14/05 01:41 PM, Inglor wrote: it's not that recommanded, it'll cause serious lag with a lot of objects on the screen

Ha! Now YOU'RE pwned :).


BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:48:19 Reply

Meh, I've never had trouble.

And I'm not trying to "pwn" anyone, grow up.

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:52:39 Reply

At 9/14/05 01:48 PM, T-H wrote: Meh, I've never had trouble.

And I'm not trying to "pwn" anyone, grow up.

TOAST IS OWNED!

P <3 th, we're both just helping when we can, the main difference is that I have a loose ass and you have a tiny pen0r.
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: Drag & Drop 2005-09-14 13:53:10 Reply

At 9/14/05 01:41 PM, Inglor wrote:
At 9/14/05 01:29 PM, T-H wrote: = teh smooth
apply it to custom cursors, draggable clips etc.
it's not that recommanded, it'll cause serious lag with a lot of objects on the screen

Now I don't know what to think!!! :(


BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:55:01 Reply

Well if you want a demo, play my game. Its runs well enough on that. But for a bigger project it would lag.

I'll see you all in a week because I just requested a ban in order to get some serious work done.

BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Drag & Drop 2005-09-14 13:59:16 Reply

At 9/14/05 01:55 PM, T-H wrote: I'll see you all in a week because I just requested a ban in order to get some serious work done.

That's the best idea I've heard all day!

5$ on he posts with an alt within 24 hours
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Drag & Drop 2005-09-14 14:03:39 Reply

At 9/14/05 01:59 PM, BleeBlap wrote:
5$ on he posts with an alt within 24 hours

we're on :) but I'll bet you 10 he does in 72

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Drag & Drop 2005-09-14 14:23:02 Reply

you both gonna fail.

why aren't i banz0red yet... Denvish where are you...

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Drag & Drop 2005-09-14 14:34:25 Reply

At 9/14/05 02:23 PM, T-H wrote: why aren't i banz0red yet... Denvish where are you...

Negative is here, but next time just e-mail someone :P


Sup, bitches :)

BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Drag & Drop 2005-09-14 14:36:31 Reply

At 9/14/05 02:34 PM, -liam- wrote:
At 9/14/05 02:23 PM, T-H wrote: why aren't i banz0red yet... Denvish where are you...
Negative is here, but next time just e-mail someone :P

Emailed Den before i posted telling people : )

Just emailed neg before you posted when i noticed him.

daypack
daypack
  • Member since: Sep. 22, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Drag & Drop 2005-09-24 08:23:31 Reply

I making a simple dress-up game. I'm using a script that the same as yours, but when I test the game and click on an image and drag it drags the hole stage.

I start off with a graphic and then convert it to a button. I bring the button over to the stage the add the script to the image. I then convert the button into a movie clip. This should work as i understand it. I'm new to action-scripting so could you help?

daypack
daypack
  • Member since: Sep. 22, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Drag & Drop 2005-09-24 08:24:48 Reply

'whole'

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Drag & Drop 2005-09-24 08:26:30 Reply

Theres no need to convert the button to a movie clip, try it without doing that (CTRL+B to break up a movie clip).


Sup, bitches :)

BBS Signature
daypack
daypack
  • Member since: Sep. 22, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Drag & Drop 2005-09-27 20:32:43 Reply

Hey, thanks for the help but it still doesn't work.

Here is the code that i'm using for the dress-up:

on (press) {
startDrag ("");
}
on (release) {
stopDrag ();
}

Please help, i got it to work but then my comp crashed and i hadn't saved yet. Lost it all and i can't remember how i did it. Thanks

cherries
cherries
  • Member since: Jun. 7, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Drag & Drop 2005-09-27 21:06:14 Reply

cool

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: Drag & Drop 2005-09-27 21:07:38 Reply

Ok, this wil work much more efficently

on(press){
startDrag()
}
onClipEvent(mouseUp){
stopDrag()
}

daypack
daypack
  • Member since: Sep. 22, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Drag & Drop 2005-09-28 17:59:09 Reply

Thanks for the code, but it doesn't work. When i click on the item that i want to drag it drags the whole stage with it. Why does it do this and how can it be fixed?

I'm stil learning how action scripting works. So the chances are i'm doing something that you flash gods will laugh at.

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Drag & Drop 2005-09-28 18:13:08 Reply

At 9/28/05 05:59 PM, daypack wrote: Thanks for the code, but it doesn't work. When i click on the item that i want to drag it drags the whole stage with it. Why does it do this and how can it be fixed?

I'm stil learning how action scripting works. So the chances are i'm doing something that you flash gods will laugh at.

Which code are you using, and are you sure you're pasting it in the right place (select the MC on stage, Press F9, paste actions.)?


- - Flash - Music - Images - -

BBS Signature
daypack
daypack
  • Member since: Sep. 22, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Drag & Drop 2005-09-28 18:19:03 Reply

I'm using the code:

on (press) {
startDrag ("");
}
on (release) {
stopDrag ();
}

I paste this into a button and then convert it to a mv. It this right? Could you tell me why the stage drags with, for future ref?

Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to AS: Drag & Drop 2005-10-17 11:38:58 Reply

I dont know if i should be posting, but I know your problem. Ive learned a few tricks in this. Well, put as instance name in one of your clothes "cloth1" then paste THIS code. (I just edited yours, it should work)

on(press){
startDrag("cloth1");
}
on(release){
stopDrag();
}

Scripted on the BBS, so i dont know if it would work.

Well, the " " " are to put instance names there, So, you put the instance name of the cloth you want to drag and it will work, hopefully. At least thats what my common sense and very"bad" knowledge on Actionscript tell me. Hope i helped.

Devenger
Devenger
  • Member since: Dec. 24, 2004
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Drag & Drop 2005-10-17 12:49:10 Reply

Am I allowed to post how you'd do a (not very economical) fade to the mouse?

onClipEvent(enterFrame){
this._x += (this._x- _root._xmouse)/16;
this._y += (this._y- _root._ymouse)/16;
}

I suppose some fella will flame me for it, but it should at least work (although I did write it from memory!)

Change the divisions to change how fast it will reach it's target (divided by 1 = accurate mouse tracking, i.e. pointless)


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Harpo-1
Harpo-1
  • Member since: Jan. 15, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Drag & Drop 2005-10-27 01:34:22 Reply

At 10/17/05 12:49 PM, Devenger wrote: Am I allowed to post how you'd do a (not very economical) fade to the mouse?

onClipEvent(enterFrame){
this._x += (this._x- _root._xmouse)/16;
this._y += (this._y- _root._ymouse)/16;
}

*Cough*
onClipEvent(enterFrame){
this._x -= (this._x- _root._xmouse)/16;
this._y -= (this._y- _root._ymouse)/16;
}
*Cough*

The problem was, the "_x +=" will make the movie clip run from the mouse, but it will however make a cool "herding" thingy : )


2b r not2b lawl

BBS Signature
Tolomus
Tolomus
  • Member since: Nov. 17, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Drag & Drop 2006-04-08 20:56:13 Reply

Hopefully this hasn't already been answered in another thread, but here goes...

I've added some time ago a drag and drop script to an instance.... it works fine. How can I make it so that you cannot drag it (using a hittest I assume) over another MC? That is... I know you can define a section using stage widths and heights to stop an MC being dragged past a certain point... but, is it possible to have it so that, while dragging the MC, it cannot be made to overlap another irregularly shaped MC, so instead of overlapping, it gets 'stuck' on the edge...

Yes I know I'm not explaining myself very well... and sorry if this is a noob question.

Any help much appreciated.

Tolomus
Tolomus
  • Member since: Nov. 17, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Drag & Drop 2006-04-09 06:20:21 Reply

.......................... anyone?

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Drag & Drop 2006-04-09 06:23:27 Reply

onClipEvent(enterFrame){
if(dragging){
for(a in _root){
if(_root[a] instanceof MovieClip && _root[a].hitTest(this)){
stopDrag();
}
}
}
}
on(press){
startDrag(this), dragging=true;
}
on(release){
stopDrag(), dragging=false;
}


Sup, bitches :)

BBS Signature
Tolomus
Tolomus
  • Member since: Nov. 17, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Drag & Drop 2006-04-09 12:07:54 Reply

Hmhmhmh.... maybe I'm being thick and doing somethign wrong, but this doesn't seem to work for me.......

Any other help? Or an explanation of this script?

Thanks again.

Tolomus
Tolomus
  • Member since: Nov. 17, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Drag & Drop 2006-04-09 17:44:11 Reply

.................................... anyone?