00:00
00:00
Newgrounds Background Image Theme

SpeakyDooman just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Drag & Drop

10,158 Views | 55 Replies
New Topic Respond to this Topic

AS: Drag & Drop 2005-07-26 17:30:44


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

Response to AS: Drag & Drop 2005-09-14 13:29:03


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.

Response to AS: Drag & Drop 2005-09-14 13:41:06


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

Response to AS: Drag & Drop 2005-09-14 13:45:44


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

Response to AS: Drag & Drop 2005-09-14 13:48:19


Meh, I've never had trouble.

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

Response to AS: Drag & Drop 2005-09-14 13:52:39


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.

Response to AS: Drag & Drop 2005-09-14 13:53:10


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

Response to AS: Drag & Drop 2005-09-14 13:55:01


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.

Response to AS: Drag & Drop 2005-09-14 13:59:16


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

Response to AS: Drag & Drop 2005-09-14 14:03:39


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

Response to AS: Drag & Drop 2005-09-14 14:23:02


you both gonna fail.

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

Response to AS: Drag & Drop 2005-09-14 14:34:25


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

Response to AS: Drag & Drop 2005-09-14 14:36:31


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.

Response to AS: Drag & Drop 2005-09-24 08:23:31


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?

Response to AS: Drag & Drop 2005-09-24 08:24:48


'whole'

Response to AS: Drag & Drop 2005-09-24 08:26:30


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

Response to AS: Drag & Drop 2005-09-27 20:32:43


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

Response to AS: Drag & Drop 2005-09-27 21:07:38


Ok, this wil work much more efficently

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

Response to AS: Drag & Drop 2005-09-28 17:59:09


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.

Response to AS: Drag & Drop 2005-09-28 18:13:08


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

Response to AS: Drag & Drop 2005-09-28 18:19:03


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?

Response to AS: Drag & Drop 2005-10-17 11:38:58


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.

Response to AS: Drag & Drop 2005-10-17 12:49:10


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.

Response to AS: Drag & Drop 2005-10-27 01:34:22


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

Response to AS: Drag & Drop 2006-04-08 20:56:13


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.

Response to AS: Drag & Drop 2006-04-09 06:20:21


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

Response to AS: Drag & Drop 2006-04-09 06:23:27


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

Response to AS: Drag & Drop 2006-04-09 12:07:54


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.

Response to AS: Drag & Drop 2006-04-09 17:44:11


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

Response to AS: Drag & Drop 2006-04-09 18:36:45


Sorry to keep bumping... I know I've been answered but it doesn't seem to work for me... I've tried playing around with the code, but no luck...

Again, any help much appreciated.