00:00
00:00
Newgrounds Background Image Theme

kcgreenn 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: Follow Mouse

1,434 Views | 22 Replies
New Topic Respond to this Topic

AS: Follow Mouse 2006-06-11 09:19:36


Okay, this is my first try at an Action Script lesson, but I hope you learn something from it. I'm going to show you how to make a movie clip follow your mouse, and no, this is not a startDrag("")/stopDrag tutorial!

Start off by making a movie clip, call it anything you want. Drag it out on stage.
Click on it, make sure it says Actions - Movie Clip on the actions bar. If it does, open the actions bar up.

Start the code off by putting:

onClipEvent(enterFrame){your code will go here }

The onClipEvent(enterFrame) is telling flash, when the movie clip loads up onto the frame, the code in the braces will happen. The braces are essential, or the code will not work, as you might already know.

Okay, so let's add on a little more.

onClipEvent(enterFrame){this._x = (_root._xmouse)}

this._x = (_root.xmouse) is telling flash that the movie clip will horizontally follow the mouse, almost like an automatic slidebar.

Now let's add some more.

onClipEvent(enterFrame){this._x = (_root.xmouse);
this._y = (_root.ymouse); }

The code that I just added is exactly like the previous code but now the Mc will follow the mouse vertically as well. Making it a perfect dragged movie clip.

If you want to make it kinda like runescape movement where your mouse goes to a spot when you click on it, you can, just change the clip event to mouseDown.

onClipEvent(mouseDown){this._x = (_root.xmouse);
this._y = (_root.ymouse); }

You can really make some funky stuff happen if you experiment with it. Try out some of these codes.

onClipEvent(enterFrame){this._y= (_root.xmouse);
}

onClipEvent(enterFrame){this._x = (_root.ymouse);
}

It's like a slide bar, but the mc is following the _ymouse with a _x motion and vice-versa.
Good Luck!

Response to AS: Follow Mouse 2006-06-11 09:21:55


Good tut, but unfortunately it's been covered already. Also, put a link to AS: Main. Good try though.

Response to AS: Follow Mouse 2006-06-11 09:23:30


http://www.newground../topic.php?id=229808

Here's a link to the new AS: Main.

I really wish It would be put there.......

Oh well.....

Response to AS: Follow Mouse 2006-06-11 09:46:30


Yes, I do, it's just I explained it in my own way. If we were all the same the world would come to an end....maybe......(Thinking to self: Shut up Sniffy)

Response to AS: Follow Mouse 2006-06-11 09:47:18


At 6/11/06 09:24 AM, pyro111 wrote: Wow. You have no idea what you are doing do you? Well I made a better code for you.

Thats exactly he same code as the topic starter used.

Response to AS: Follow Mouse 2006-06-11 09:50:03


At 6/11/06 09:48 AM, pyro111 wrote: And also, now that im on that topic, you dont need 2 enterFrames for lat last part.

They're meant to be 2 examples of codes. They weren't meant to be used at the same time. You're not on the ball today pyro :P

Response to AS: Follow Mouse 2006-06-11 09:51:30


At 6/11/06 09:50 AM, Cybex wrote:
At 6/11/06 09:48 AM, pyro111 wrote: And also, now that im on that topic, you dont need 2 enterFrames for lat last part.
They're meant to be 2 examples of codes. They weren't meant to be used at the same time. You're not on the ball today pyro :P
D

Also, isnt that supposed to be _xmouse?= you used _root.xmouse and _root.ymouse. Shows how many people actually read it. :P

Response to AS: Follow Mouse 2006-06-11 09:55:25


At 6/11/06 09:24 AM, pyro111 wrote: Wow. You have no idea what you are doing do you? Well I made a better code for you.

Wow, your cool. You basically wrote the SAME CODE as the topic starter did! He just explained it without the quote that you put in.

Response to AS: Follow Mouse 2006-06-11 09:56:13


Great tut there, buddy! Easy, and well explained... Keep it up

Response to AS: Follow Mouse 2006-06-11 09:57:59


Wow, I guess I do have some followers that'll back me up....haha in your face guy who was critisising my work...

Response to AS: Follow Mouse 2006-06-11 10:28:57


At 6/11/06 09:57 AM, sniffy_gerbil wrote: Wow, I guess I do have some followers that'll back me up....haha in your face guy who was critisising my work...

Well... aren't we all supposed to criticize? Bad or good? That is what the whole point of Newgrounds is, criticizing... people's flashes and whatchamadoohickeys. This is in the flash forum so I think it's criticizable.

0

Response to AS: Follow Mouse 2006-06-11 10:29:00


At 6/11/06 09:19 AM, sniffy_gerbil wrote: onClipEvent(enterFrame){this._x = (_root.ymouse);
}

Hmm, that wouldn't work.
You need _root._ymouse;


BBS Signature

Response to AS: Follow Mouse 2006-06-11 10:34:48


At 6/11/06 10:29 AM, GuyWithHisComp wrote:
At 6/11/06 09:19 AM, sniffy_gerbil wrote: onClipEvent(enterFrame){this._x = (_root.ymouse);
}
Hmm, that wouldn't work.
You need _root._ymouse;

Yes it does, I tried it myself.

Response to AS: Follow Mouse 2006-06-11 10:36:51


Well... aren't we all supposed to criticize? Bad or good? That is what the whole point of Newgrounds is, criticizing... people's flashes and whatchamadoohickeys. This is in the flash forum so I think it's criticizable.

0

Yeah, you're right, I was just having one of my few proud moments.

Response to AS: Follow Mouse 2006-06-11 10:40:29


At 6/11/06 10:36 AM, pyro111 wrote: Well then, im gonna point out everything that is wrong with your code(s).

It does work, it's not wrong. It still works the same. I'm teaching people what I think is right, and what works for me, I know that's how most people right it, but as long is it works, I don't care.

Wait, does actionscript have a difference between versions of flash?
I have MX 2004, maybe it doesn't work on flash eight.

Response to AS: Follow Mouse 2006-06-11 10:50:09


i dont know wat version of flash u r using, but

_root.ymouse &
_root.xmouse

do completely nuthing on mine

_root._ymouse &
_root._xmouse

on the other hand....

(and btw i did check, which is more than i guess you did)

Response to AS: Follow Mouse 2006-06-11 10:50:39


At 6/11/06 10:40 AM, sniffy_gerbil wrote: It does work, it's not wrong. It still works the same. I'm teaching people what I think is right, and what works for me, I know that's how most people right it, but as long is it works, I don't care.

See, your code ISNT wrong, but it IS unefficient. There are more proper ways of doing this, and much more easier that will only take two or three lines;

onClipEvent(enterFrame){
this._x=_xmouse;
this._y=_ymouse;
updateAfterEvent();
//for smoothness
}

Response to AS: Follow Mouse 2006-06-11 10:53:41


u dont need the update after event there - as it will update after event almost - its an extra rendering that cant be realised.

however i would do a cursor like this as follows

cursor.startDrag(true)
onMouseMove=function(){
updateAfterEvent()
}

Response to AS: Follow Mouse 2006-06-11 10:58:33


At 6/11/06 10:36 AM, pyro111 wrote: Well then, im gonna point out everything that is wrong with your code(s).

Lol pyro, you're the only wrong one here. None of the initial code was wrong. I believe xmouse and ymouse were AS1 for _xmouse and _ymouse. It doesn't make them wrong, just old. Also there's nothing wrong with the way he was putting loads of things on one line. Yes its not how it is strictly written, but it still works. If you're going to argue about him not laying it out as flash advises, i'm going to say you're wrong for doing:

onClipEvent(mouseDown){
_x=_root._xmouse;
_y=_root._ymouse;
}

instead of

onClipEvent (mouseDown) {
_x = _root._xmouse;
_y = _root._ymouse;
}

Stop trying to find faults in his code, you'll just embarass yourself :P

Response to AS: Follow Mouse 2006-06-11 10:59:39


At 6/11/06 10:50 AM, Darkfire_Blaze wrote: onClipEvent(enterFrame){
this._x=_xmouse;
this._y=_ymouse;
updateAfterEvent();
//for smoothness
}

Good ol' DFblaze ;)
updateAfterEvent(); does nothing when in onEnterFrame, only in onMouseMove so it doesn't smooth anything in your example.
And you forgot (I hope) the _root.s on _xmouse and _ymouse so you instead use the _xmouse+_x.
Proper code of yours would be:
onClipEvent(mouseMove){
this._x=_root._xmouse;
this._y=_root._ymouse;
updateAfterEvent();
}


BBS Signature

Response to AS: Follow Mouse 2006-06-11 11:06:17


At 6/11/06 10:58 AM, Cybex wrote: onClipEvent(mouseDown){
_x=_root._xmouse;
_y=_root._ymouse;
}

instead of

onClipEvent (mouseDown) {
_x = _root._xmouse;
_y = _root._ymouse;
}

Damnit! I meant:

onClipEvent (mouseDown) {
_x = _root._xmouse;
_y = _root._ymouse;
}

Response to AS: Follow Mouse 2006-06-11 11:07:44


At 6/11/06 10:59 AM, GuyWithHisComp wrote: onClipEvent(mouseMove){
this._x=_root._xmouse;
this._y=_root._ymouse;
updateAfterEvent();
}

ive used it without the _root, and ive also used updateAfterEvent on enter frames. :P Im just that bad of a coder. : D

Response to AS: Follow Mouse 2006-06-11 11:09:51


At 6/11/06 11:06 AM, Cybex wrote: Damnit! I meant:

onClipEvent (mouseDown) {
_x = _root._xmouse;
_y = _root._ymouse;
}

Ok, you win. Its impossile to indent on the bbs.