Forum Topic: AS: Drag & Drop

(5,923 views • 57 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

Inglor

Reply To Post Reply & Quote

Posted at: 7/26/05 05:30 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/26/05 05:35 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

All right! Thats much better!

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

T-H

Reply To Post Reply & Quote

Posted at: 9/14/05 01:29 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


None

Inglor

Reply To Post Reply & Quote

Posted at: 9/14/05 01:41 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

Toast

Reply To Post Reply & Quote

Posted at: 9/14/05 01:45 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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


None

T-H

Reply To Post Reply & Quote

Posted at: 9/14/05 01:48 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

Meh, I've never had trouble.

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


None

Inglor

Reply To Post Reply & Quote

Posted at: 9/14/05 01:52 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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.

None

Paranoia

Reply To Post Reply & Quote

Posted at: 9/14/05 01:53 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,697

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

None

T-H

Reply To Post Reply & Quote

Posted at: 9/14/05 01:55 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


None

BleeBlap

Reply To Post Reply & Quote

Posted at: 9/14/05 01:59 PM

BleeBlap LIGHT LEVEL 24

Sign-Up: 03/08/05

Posts: 944

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

None

Inglor

Reply To Post Reply & Quote

Posted at: 9/14/05 02:03 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

T-H

Reply To Post Reply & Quote

Posted at: 9/14/05 02:23 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

you both gonna fail.

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


None

liaaaam

Reply To Post Reply & Quote

Posted at: 9/14/05 02:34 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,530

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


None

T-H

Reply To Post Reply & Quote

Posted at: 9/14/05 02:36 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


Happy

daypack

Reply To Post Reply & Quote

Posted at: 9/24/05 08:23 AM

daypack NEUTRAL LEVEL 02

Sign-Up: 09/22/05

Posts: 7

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?


None

daypack

Reply To Post Reply & Quote

Posted at: 9/24/05 08:24 AM

daypack NEUTRAL LEVEL 02

Sign-Up: 09/22/05

Posts: 7

'whole'


None

liaaaam

Reply To Post Reply & Quote

Posted at: 9/24/05 08:26 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,530

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


None

daypack

Reply To Post Reply & Quote

Posted at: 9/27/05 08:32 PM

daypack NEUTRAL LEVEL 02

Sign-Up: 09/22/05

Posts: 7

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


None

cherries

Reply To Post Reply & Quote

Posted at: 9/27/05 09:06 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,576

cool


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 9/27/05 09:07 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

Ok, this wil work much more efficently

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


None

daypack

Reply To Post Reply & Quote

Posted at: 9/28/05 05:59 PM

daypack NEUTRAL LEVEL 02

Sign-Up: 09/22/05

Posts: 7

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.


None

Denvish

Reply To Post Reply & Quote

Posted at: 9/28/05 06:13 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

daypack

Reply To Post Reply & Quote

Posted at: 9/28/05 06:19 PM

daypack NEUTRAL LEVEL 02

Sign-Up: 09/22/05

Posts: 7

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?


None

Blaze

Reply To Post Reply & Quote

Posted at: 10/17/05 11:38 AM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

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.


None

Devenger

Reply To Post Reply & Quote

Posted at: 10/17/05 12:49 PM

Devenger NEUTRAL LEVEL 09

Sign-Up: 12/24/04

Posts: 1,103

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.


None

Harpo-1

Reply To Post Reply & Quote

Posted at: 10/27/05 01:34 AM

Harpo-1 LIGHT LEVEL 09

Sign-Up: 01/15/05

Posts: 982

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

None

Tolomus

Reply To Post Reply & Quote

Posted at: 4/8/06 08:56 PM

Tolomus NEUTRAL LEVEL 07

Sign-Up: 11/17/05

Posts: 110

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.


None

Tolomus

Reply To Post Reply & Quote

Posted at: 4/9/06 06:20 AM

Tolomus NEUTRAL LEVEL 07

Sign-Up: 11/17/05

Posts: 110

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


None

liaaaam

Reply To Post Reply & Quote

Posted at: 4/9/06 06:23 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,530

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;
}


None

Tolomus

Reply To Post Reply & Quote

Posted at: 4/9/06 12:07 PM

Tolomus NEUTRAL LEVEL 07

Sign-Up: 11/17/05

Posts: 110

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.


All times are Eastern Standard Time (GMT -5) | Current Time: 10:31 AM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!