Posts Tagged buttonMode

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.

, ,

12 Comments