Using Sprite.buttonMode and Sprite.mouseChildren

Today I found something I really did not understand very well. I had to debug a application because one button displayed the handCursor and another did not. Both use the same script and actually I didn't find out why the behaviour of this buttons was like that but I fixed it through a workaround.

The first thing I understood today is, that it's possible to receive all mouse events without using buttonMode! Simply add a listener to your objects and you can work with them.

But if you like to work with a button that contains additional sprites (which might be very useful) you definitely should take care about their behaviour. By default mouse events will be dispatched by the lowest sprite, exept you set mouseChildren to false...

Have a look at this little code example:

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.events.Event;
  4.     import flash.events.MouseEvent;
  5.  
  6.     public class ButtonModeTest extends Sprite
  7.     {
  8.         public function ButtonModeTest()
  9.         {
  10.             run();
  11.         }
  12.        
  13.         private function run(): void
  14.         {
  15.             //-- Start listening
  16.             addEventListener( MouseEvent.CLICK, onMouseEvent );
  17.             addEventListener( MouseEvent.MOUSE_OVER, onMouseEvent );
  18.             addEventListener( MouseEvent.MOUSE_OUT, onMouseEvent );
  19.            
  20.             //-- Button 1 on the main stage
  21.             var button1: Sprite = createButton( 1 );
  22.             addChild( button1 );
  23.            
  24.             //-- Button 2 within the container
  25.             var button2: Sprite = createButton( 2 );
  26.             var button2Container: Sprite = new Sprite();
  27.             button2Container.addChild( button2 );
  28.             button2Container.x = 150;
  29.             button2Container.name = "button2Container";
  30.             addChild( button2Container );
  31.            
  32.             // By default the mouse events will be dispatched by the buttons themself
  33.             // (the lowest sprites)
  34.            
  35.             // Setting containers buttonMode to true doesn't force the container to
  36.             // dispatch the mouse events ( so what the hell is button mode?)
  37.             button2Container.buttonMode = true;
  38.  
  39.             // Setting containers useHandCursor to true only works when it's buttonMode
  40.             // is also true. But the events will still be dispatched from the lowest
  41.             // sprite (even its a button!?!)
  42.             button2Container.useHandCursor = true;
  43.            
  44.             // Only setting mouseChildren to false forces the container to dispatch
  45.             // the mouse events
  46.             button2Container.mouseChildren = false;
  47.         }
  48.        
  49.         private function createButton( index: uint ): Sprite
  50.         {
  51.             var button: Sprite = new Sprite();
  52.             button.name = "button" + index;
  53.             button.graphics.beginFill( 0, 1 );
  54.             button.graphics.drawRect( 0, 0, 100, 100 );
  55.             button.graphics.endFill();
  56.            
  57.             return button;
  58.         }
  59.        
  60.         private function onMouseEvent( event: Event ): void
  61.         {
  62.             trace( "Event (" + event.type + ") dispatched from : " + event.target.name );
  63.         }
  64.     }
  65. }

0 Responses to “Using Sprite.buttonMode and Sprite.mouseChildren”


  1. No Comments

Leave a Reply