useHandCursor and Sprites in ActionScript 3

Here is another thing I was trying to do today, but I couldn’t find an answer on the net. I was quite simple actually; I was just wanting to make the mouse display the hand cursor when it rolled over some Sprites. Since Sprites don’t have timelines, you save memory in comparison to MovieClips. That’s one of the major change in ActionScript 3 and it gives even more meaning to doing your animations using code.

So one of the things you might want to do is add some mouse functionality to your Sprites and let the user know about it by displaying the hand cursor. In ActionScript 2, that was quite easy. You just had to implement a mouse event function like onRelease, onRollover etc and that was it. No need to specify it, the cursor would automatically change to a hand cursor. In ActionScript there are no such functions anymore, you have to make a listener to catch the events and then your event handler functions. So you have to specify when you want the mouse cursor to display the hand.

Now that looks easy, just turn the useHandCursor of the Sprite to true. Yes, that’s one part of it, but there is another step that is not so obvious. You have to also set the buttonMode property of the Sprite to true (in which case you don’t have to set the other property). Here is some example code for it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package {
  import flash.display.Sprite;
  import flash.events.*;
  public class thumb extends Sprite{
    public function thumb(image:String, X:int, Y:int)
    {
      x = X;
      y = Y;
      buttonMode = true;
      addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
    }
    function handleMouseUp(event:Event){
      //your code here (simular to onRelease in actionScript 2)
    }
  }
}

I hope it saved you the hour it took me to find out.

, ,

  1. #1 by g-munee - September 30th, 2007 at 20:20

    just building my first as3 scrollbar this weekend, your post helped out. over all i’m enjoying as3 ( i’m about 3-4 months in, ) but it’s killing me how long it takes me to re-learn some of the things that were basic in as2.

  2. #2 by dgelineau - October 1st, 2007 at 00:47

    Glad it helped, as soon as I am done with a template I am doing, I’ll also make my own as3 scrollbar

  3. #3 by Izikon - November 13th, 2007 at 12:44

    This will not work if the sprite has children
    you have to use the
    mouseChildren command and set it to False

  4. #4 by Dr. Bence - August 22nd, 2008 at 08:47

    Thanks a lot. It helped.

  5. #5 by igi - June 27th, 2009 at 03:13

    Thanks! Its really help!!!

  6. #6 by Maxweel - January 24th, 2010 at 10:02

    “I hope it saved you the hour it took me to find out.”

    It did indeed!
    Thanks

  7. #7 by Christian M. Cepel - April 30th, 2010 at 16:49

    Me as well. Thank you.

  8. #8 by music quotes - February 2nd, 2011 at 14:40

    thanks for sharing this info!

  9. #9 by Zachary Draper - April 13th, 2011 at 16:11

    Thanks for sharing, and a special thanks to Izikon for the bit about mouseChildren. That was the little piece of critical information I needed to make everything work.

  10. #10 by Eugene Prokopovich - November 7th, 2011 at 12:49

    You are really cool. Thanks!

  11. #11 by Jose Alejandro Realz - August 22nd, 2012 at 22:48

    I am in flash, and this property don’t working with a Movieclip as instance

(will not be published)
Subscribe to comments feed