Forum Topic: AS3: Dragging Objects

(1,252 views • 7 replies)

This topic is 1 page long.

<< < > >>
Thinking

LilFugitive

Reply To Post Reply & Quote

Posted at: 11/8/07 09:35 AM

LilFugitive EVIL LEVEL 16

Sign-Up: 06/01/06

Posts: 568

What am I going to explain?

I'll explain how to make the code used for dragging objects around. You can use this code for dress up games, but maybe you can think up an other use for it ;)

How are we going to do it?

First draw an object, make it a movieclip give it the instance name object. Note that you can also give an other name to it, but then you'll have to also change it in the code to the name you chose.

After that we'll decide what type of Listeners we want to add...
We want flash to know when the mouse is down so we add this Listener on the object.

object.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);

Of course we also want flash to know when the user releases his mouse so we'll add this listener on the listener

object.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

As last listener we'll add a listener that will continuously keep the position of the mouse the same as the object, as long as the left mouse button is down! So we'll use this listener on the object:

object.addEventListener(Event.ENTER_FRAME, dragging);

We also want that it doesn't matter where the person clicks on the object, so he can dragg it from any point. We'll use a point variable, I'm calling it distance, but you can call it anything, remember to change the code in that case!

Now we'll say that the distance variable point is null, it doesn't have a value.

var distance: Point = null;

Now we'll start adding the functions to the EventListeners, the first one is the MOUSE_DOWN listener. When the mouse is down the variable point distance will get the position of where there's been clicked.

function startDragging (event:MouseEvent){
distance = new Point (event.localY, event.localX);
}

Afterwards we'll add the code that when the mouse button is released, in other words up, the variable distance point is null.

function stopDragging(event:MouseEvent) {
	distance = null;
}

As last code we'll add the function to the ENTER_FRAME listener:

function dragging(event:Event) {
if (distance != null) { 
object.x = mouseX - distance.x;
object.y = mouseY - distance.y;
}
}

Note that without the - distance.x and -distance.y the center of the object would pop to the mouse, try it yourself ;)

To help you guys out here's the code all together:

object.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
object.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
object.addEventListener(Event.ENTER_FRAME, dragging;

var distance:Point = null;


function startDragging(event:MouseEvent) {
distance = new Point(event.localX, event.localY);
}

function stopDragging(event:MouseEvent) {
distance = null;
}

function dragging(event:Event) {
if (distance != null) { 
object.x = mouseX - distance.x;
object.y = mouseY - distance.y;
}
}

And as last here's a download to the .fla file that you could use.

If you're having problems leave a comment or PM me.
Also leave a comment or PM me if I made a mistake.

Ruining your life since 2006

BBS Signature

None

LilFugitive

Reply To Post Reply & Quote

Posted at: 11/8/07 01:24 PM

LilFugitive EVIL LEVEL 16

Sign-Up: 06/01/06

Posts: 568

At 11/8/07 10:19 AM, LCurtis wrote: You do realize they didnt remove the startDrag() and stopDrag() functions from AS3 right?

Im not so sure you need to be doing AS3 tutorials

Oh.... Holy shit, so I did this for nothing basically XD

Ruining your life since 2006

BBS Signature

None

DragonFruit-Rock

Reply To Post Reply & Quote

Posted at: 11/8/07 04:09 PM

DragonFruit-Rock EVIL LEVEL 13

Sign-Up: 12/31/05

Posts: 1,248

no shit bitch everyon uses
on(press){
startDrag(this, true);
}
on(release){
stopDrag();
}
lol

If a man that always tells the truth comes up to you and says that another man always tells lies, and the man who always lies come up to you and says "I'm lying", then is he?

BBS Signature

None

mmmlolipops

Reply To Post Reply & Quote

Posted at: 11/8/07 04:12 PM

mmmlolipops EVIL LEVEL 02

Sign-Up: 11/07/07

Posts: 180

yep. this is pointless. unless you need to use an alternative for some oddball reason

thanks, dr34m3r, for the sig!

BBS Signature

None

LilFugitive

Reply To Post Reply & Quote

Posted at: 11/9/07 12:03 PM

LilFugitive EVIL LEVEL 16

Sign-Up: 06/01/06

Posts: 568

Sorry people, I just didn't know there was still a startDragg and stopDragg... xD

FORUM MODERATORS CAN REMOVE THIS THREAD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Ruining your life since 2006

BBS Signature

None

souled

Reply To Post Reply & Quote

Posted at: 11/9/07 12:05 PM

souled NEUTRAL LEVEL 14

Sign-Up: 09/18/06

Posts: 1,554

At 11/9/07 12:03 PM, LilFugitive wrote: Sorry people, I just didn't know there was still a startDragg and stopDragg... xD

FORUM MODERATORS CAN REMOVE THIS THREAD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

nah, its a good alternative.


None

XBigTK13X

Reply To Post Reply & Quote

Posted at: 1/6/08 09:15 AM

XBigTK13X LIGHT LEVEL 28

Sign-Up: 08/04/05

Posts: 370

At 11/8/07 04:12 PM, mmmlolipops wrote: yep. this is pointless. unless you need to use an alternative for some oddball reason

Actually, it DOES have a good alt. use. For some reason if you have a hierarchy in which multiple objects are draggable, it takes more lines of code to prevent glitches using the traditional startDrag/stopDrag methods. However, using a homebrew drag class eliminates the problem. I'm not sure what Adobe effed up...but they should really get around to fixing it ^_^.


None

archeris

Reply To Post Reply & Quote

Posted at: 7/16/08 08:45 PM

archeris EVIL LEVEL 19

Sign-Up: 04/01/06

Posts: 65

At 11/8/07 04:09 PM, DragonFruit-Rock wrote: no shit bitch everyon uses
on(press){
startDrag(this, true);
}
on(release){
stopDrag();
}
lol

Your right noob, but you know what i the difference between this, that this crappy script uses only in As2!


All times are Eastern Standard Time (GMT -5) | Current Time: 07:22 AM

<< Back

This topic is 1 page long.

<< < > >>
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!