Forum Topic: AS3 : Custom Cursor

(4,088 views • 23 replies)

This topic is 1 page long.

<< < > >>
None

Jindo

Reply To Post Reply & Quote

Posted at: 5/3/07 02:59 AM

Jindo LIGHT LEVEL 22

Sign-Up: 08/30/05

Posts: 1,789

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

:D


None

Sid1120

Reply To Post Reply & Quote

Posted at: 5/3/07 06:01 AM

Sid1120 EVIL LEVEL 15

Sign-Up: 06/13/04

Posts: 81

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


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 5/3/07 07:09 AM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,838

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.


Angry

Gorilla-Studios

Reply To Post Reply & Quote

Posted at: 5/3/07 07:39 AM

Gorilla-Studios FAB LEVEL 26

Sign-Up: 11/18/05

Posts: 1,702

I rather stick with AS2. Good job though!

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

BBS Signature

None

Alphabit

Reply To Post Reply & Quote

Posted at: 5/3/07 09:18 AM

Alphabit NEUTRAL LEVEL 09

Sign-Up: 02/14/06

Posts: 4,061

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.


Happy

Depredation

Reply To Post Reply & Quote

Posted at: 5/3/07 11:14 AM

Depredation LIGHT LEVEL 17

Sign-Up: 09/05/05

Posts: 4,782

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

None

Jindo

Reply To Post Reply & Quote

Posted at: 5/3/07 11:20 AM

Jindo LIGHT LEVEL 22

Sign-Up: 08/30/05

Posts: 1,789

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

:D


None

ShittyFlash

Reply To Post Reply & Quote

Posted at: 5/6/07 04:21 PM

ShittyFlash EVIL LEVEL 05

Sign-Up: 04/13/07

Posts: 62

I just got flash cs3 and it is teh weirdyz

can't apply code to mc. D:

None

Jindo

Reply To Post Reply & Quote

Posted at: 5/6/07 05:13 PM

Jindo LIGHT LEVEL 22

Sign-Up: 08/30/05

Posts: 1,789

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

:D


None

TheGreatPatten

Reply To Post Reply & Quote

Posted at: 5/6/07 05:21 PM

TheGreatPatten NEUTRAL LEVEL 20

Sign-Up: 06/17/06

Posts: 2,983

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

None

Sid1120

Reply To Post Reply & Quote

Posted at: 5/6/07 07:11 PM

Sid1120 EVIL LEVEL 15

Sign-Up: 06/13/04

Posts: 81

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.


None

clixels

Reply To Post Reply & Quote

Posted at: 8/2/07 10:19 PM

clixels LIGHT LEVEL 12

Sign-Up: 05/23/07

Posts: 295

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?


None

EviLudy

Reply To Post Reply & Quote

Posted at: 8/2/07 10:37 PM

EviLudy LIGHT LEVEL 39

Sign-Up: 08/17/02

Posts: 4,527

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

Swing a Little more!
.. ROCK OUT!

BBS Signature

None

clixels

Reply To Post Reply & Quote

Posted at: 8/2/07 10:48 PM

clixels LIGHT LEVEL 12

Sign-Up: 05/23/07

Posts: 295

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?


Happy

LilFugitive

Reply To Post Reply & Quote

Posted at: 9/28/07 03:48 PM

LilFugitive EVIL LEVEL 16

Sign-Up: 06/01/06

Posts: 568

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

None

pobah

Reply To Post Reply & Quote

Posted at: 9/28/07 03:52 PM

pobah LIGHT LEVEL 10

Sign-Up: 05/02/06

Posts: 260

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

just not me

i don't use this account anymore :)


None

bcapecci

Reply To Post Reply & Quote

Posted at: 3/12/08 06:19 PM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 296

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.


None

Wolfears2

Reply To Post Reply & Quote

Posted at: 6/3/08 02:49 PM

Wolfears2 EVIL LEVEL 08

Sign-Up: 10/20/07

Posts: 862

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

None

Wolfears2

Reply To Post Reply & Quote

Posted at: 6/3/08 02:52 PM

Wolfears2 EVIL LEVEL 08

Sign-Up: 10/20/07

Posts: 862

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

None

CircleNineStudios

Reply To Post Reply & Quote

Posted at: 6/3/08 02:52 PM

CircleNineStudios EVIL LEVEL 09

Sign-Up: 07/21/07

Posts: 304

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

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 6/3/08 02:53 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

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

Blushing

Wolfears2

Reply To Post Reply & Quote

Posted at: 6/4/08 12:57 AM

Wolfears2 EVIL LEVEL 08

Sign-Up: 10/20/07

Posts: 862

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

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 6/4/08 02:08 AM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

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

None

Vudrok

Reply To Post Reply & Quote

Posted at: 10/16/08 12:43 PM

Vudrok NEUTRAL LEVEL 01

Sign-Up: 10/16/08

Posts: 1

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


All times are Eastern Standard Time (GMT -5) | Current Time: 09:58 PM

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