00:00
00:00
Newgrounds Background Image Theme

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

AS3 : Custom Cursor

8,884 Views | 23 Replies
New Topic Respond to this Topic

AS3 : Custom Cursor 2007-05-03 02:59:14


AS3: Main

I've checked and I don't think anyone's made this yet, so why not? :P.

---

So CS3's out and now everyone's trying to learn AS3, it's a pain, what should only take a few lines of code will now take alot more, but here's how I found the best way to make a custom cursor (so far, it's all i tried, so there may be better ways).

First, make a movieclip, give it an instance name of 'cursor', draw any kind of arrow inside it and now for the code:

stage.addEventListener(Event.ENTER_FRAME, moveMouse);

This creates a listener on the stage, the event is Event.ENTER_FRAME, which is the new on.EnterFrame function, the listener calls the moveMouse function constantly because of it.

function moveMouse(Event) {

This creates the moveMouse function, the Event part is important, since you'll get a compile error otherwise :P.

Mouse.hide();

Thankfully somethings are the same, this hides the mouse.

cursor.x = mouseX;
cursor.y = mouseY;

This sets the position of the cursor, no longer do you need a _ symbol before a movieclip's parameters :P.

}

This is the end of the function.

---

Testing it should hopefully give you a working custom cursor.

Hope this helps and works (atleast it did for me).

Response to AS3 : Custom Cursor 2007-05-03 06:01:45


Somewhat confusing for something very simple, I'd suggest giving the entire code since it is quite small and then explaining

package bobby.ui {
import flash.display.Sprite; // I use Sprite, which is same as MC but no timeline
import flash.display.Shape;
import flash.display.Graphics;

public class CustomCursor extends Sprite {
private var _cursor:Shape;

public function CustomCursor() {
_cursor = drawShape();
cursor.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

// This Function will return a shape that looks like a cursor, could be replaced with a script to load an image or something, but I like it plain and simple.
private function drawShape():Shape {
var shape:Shape = new Shape();
shape.graphics.lineStyle(1);
shape.graphics.moveTo(0,10);
shape.graphics.lineTo(0,0);
shape.graphics.lineTo(10,0);
return shape;
}

// This class is essential, it's the one being called continually onEnterFrame. HEre, I move the _cursor, but I could also simply use this.x & this.y why, as the _cursor was itinially set at 0,0 in this very Class. using _cursor.x (& y) means I need to have this Class at 0,0 positioning.
private function onEnterFrame(event:Event):void {
_cursor.x = mouseX; // It's spelled this way, not Mouse.y; make Mouse.y if you want a mistake!
_cursor.y = mouseY;
}
}
}

Wrote this very quickly as I had 5mins before going off to work. Will fix it and make as a Thread on it's own when I come back

Response to AS3 : Custom Cursor 2007-05-03 07:09:26


nice tut :D
Damn, AS3 is soooo much different than AS2 :(


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

Response to AS3 : Custom Cursor 2007-05-03 07:39:26


I rather stick with AS2. Good job though!


Gorilla Studios || A game is like sex, its better when its free

BBS Signature

Response to AS3 : Custom Cursor 2007-05-03 09:18:21


You forgot to talk about the MOUSE_LEAVE event.
It's pretty important because now, thanks to AS3, we are able to detect when the mouse leaves the flash... Which means that we can hide the custom cursor once the mouse leaves.

I'll just give a short example:

addEventListener(Event.MOUSE_LEAVE, hideCustomCursor);

function hideCustomCursor(event:Event):void {
mycustomcursor.visible=false
}

... AS3 doesn't have a MOUSE_ENTER event, but that's OK, you can just use MOUSE_MOVE to set the visibility of the customcursor back to true.


Bla

Response to AS3 : Custom Cursor 2007-05-03 11:14:26


Hmm, I'm starting to understand the concept of AS3 now. So basically, it now has to be OOP? So you can't just do...

addEventListener(ENTER_FRAME){
//blahdy blah
}

... does it all have to be called using functions? I'm not complaining at all, i only really use functions, but i haven't got CS3, so i can't play around with it :(.


BBS Signature

Response to AS3 : Custom Cursor 2007-05-03 11:20:50


@Alphabit: Wow, I didn't know that, that's actually pretty handy, I remember leaving flashes open and only realising it because my mouse was flashing.

@Depredation: Yep, we have to have EventHandlers for everything now :S, but once you're used to it, learning the actual language isn't so hard.

Thanks for your comments :).

Response to AS3 : Custom Cursor 2007-05-06 16:21:23


I just got flash cs3 and it is teh weirdyz

can't apply code to mc. D:

Response to AS3 : Custom Cursor 2007-05-06 17:13:55


^ Yep, you gotta do all the coding on the frames ;).

Response to AS3 : Custom Cursor 2007-05-06 17:21:11


U should provided examples of it, for dumb people like me


Now with a trim fit between the legs, giving your baby the freedom to twist, turn, crawl, and walk. With Triple leak protection.

BBS Signature

Response to AS3 : Custom Cursor 2007-05-06 19:11:56


At 5/6/07 05:21 PM, TheGreatPatten wrote: U should provided examples of it, for dumb people like me

I did give an exemple, scroll up to see a Class I wrote to make a CustomCursor.

Response to AS3 : Custom Cursor 2007-08-02 22:19:44


I have a custom cursor that has a hitTest on it for when it hits a certain object, the frame changes, but when that happens, I get an extremely weird output. Any thoughts?

Specs:
Custom cursor has Mouse.hide();
next frame has Mouse.show();
no syntax problems

And just one more question, I gives me the error message and since it still lets me go on, when I replay the frame with a mouse hide action, it won't. Is there a way to hide it again.


Your mission is to fail this mission, do you accept?

Response to AS3 : Custom Cursor 2007-08-02 22:37:02


These kind of topics make me hate AS3 =( I'm so not learning that shit.


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to AS3 : Custom Cursor 2007-08-02 22:48:25


Why not? Its a great programming language and its a lot easier to learn than you think.


Your mission is to fail this mission, do you accept?

Response to AS3 : Custom Cursor 2007-09-28 15:48:23


At 8/2/07 10:37 PM, EviLudy wrote: These kind of topics make me hate AS3 =( I'm so not learning that shit.

If you really have problems learning AS3 I recommend you buy a book


Ruining your life since 2006

BBS Signature

Response to AS3 : Custom Cursor 2007-09-28 15:52:21


AS 2 is alot easyer for me and im used to it, but this might help others!

just not me

I like to play chess with bald men in the park...although it's hard to find 32 of them :)

Earn Some Easy Money (well...more like easy points to buy stuff or get money)

Response to AS3 : Custom Cursor 2008-03-12 18:19:02


At 5/3/07 07:09 AM, sk8more wrote: nice tut :D
Damn, AS3 is soooo much different than AS2 :(

umm nope. there not terribly different. i recently converted my games to as3 and 70%+ of the code was the same. games for the most part are programmed in the same ways as before just as3 is more oop oriented.

Response to AS3 : Custom Cursor 2008-06-03 14:49:53


If I enter this code to the frame, an output box tells me:
1120: acces of undefined property: cursor.x = mouse.X;
Anyone know what's up?


Click my sig for a hilarious thread.

BBS Signature

Response to AS3 : Custom Cursor 2008-06-03 14:52:11


made the m in Mouse capitalised and know it says I'm accesing a "possibly undefined..." something-or-other. error 1119, by the way.


Click my sig for a hilarious thread.

BBS Signature

Response to AS3 : Custom Cursor 2008-06-03 14:52:49


Haha, wow this is old. Anyway, it's not mouse.X it's mouseX (no dot). Also, make sure the instance of cursor is named on the stage just in case.


"You got treated." -Halvgoeden on Halo

"Wait for it...SNOINK!!!" -Halvgoeden was on WoW

BBS Signature

Response to AS3 : Custom Cursor 2008-06-03 14:53:00


At 6/3/08 02:49 PM, Wolfears2 wrote: If I enter this code to the frame, an output box tells me:
1120: acces of undefined property: cursor.x = mouse.X;
Anyone know what's up?

Why did you enter a dot between 'mouse' and 'X'?

If you use the original code posted in the first post in this thread you just have to have a MovieClip named cursor to make it work.


BBS Signature

Response to AS3 : Custom Cursor 2008-06-04 00:57:27


At 6/3/08 02:53 PM, GuyWithHisComp wrote: Why did you enter a dot between 'mouse' and 'X'?

If you use the original code posted in the first post in this thread you just have to have a MovieClip named cursor to make it work.

Hey, I'm used to AS2. Don't laugh at me =P


Click my sig for a hilarious thread.

BBS Signature

Response to AS3 : Custom Cursor 2008-06-04 02:08:12


At 6/4/08 12:57 AM, Wolfears2 wrote: Hey, I'm used to AS2. Don't laugh at me =P

The same would apply to AS2 though.

The difference is that mouseX is _xmouse in AS2. No dot there.


BBS Signature

Response to AS3 : Custom Cursor 2008-10-16 12:43:43


Well, Im just gonna say, that I love to learn everything as it comes out, I've been programming since Pascal and I got a pretty high background on C, C++, since I love web development I have learn tons of scripting languages since almost none deserve to be called Programming Language, as3 is almost so close to be a programming language, I like to recommend my friends good books to learn about this stuff and I've been reply with things like "i hate flash cs3 i will not learn as3" and then it makes me think that in sort of a way the web development path will be cleared of "bad practice developers" and only the good ones will make it through, so thumbs UP for adobe not to let those mediocres developers keep doing horror things...