[Forward]AS3 Double Click and mouseChildren

From http://www.charglerode.com/blog/?p=54

 

As an interface and game developer, I find myself struggling with Flash as a development platform because of it’s lack of support for common inputs. Flash has grown over the last decade as a serious casual gaming platform, but has only recently begun to support such inputs as mouse wheel and double click. Don’t even get me started on right button click and Adobe’s obvious decision to leave it as it is. I digress.

Anyhow, the real reason for this post was to expose a little caveat with the double click mouse event in AS3. I fought with this and dug through the documentation for many hours until I finally found the solution. Setting up the double click mouse event is the same as any other mouse event:

?
1
myButton.addEventListener( MouseEvent.DOUBLE_CLICK, myHandler );

I excluded the handler function because that part is irrelevant to the solution. Now, if you were to try this by itself, you would notice that this doesn’t actually work. Ok, so if you have a simple clip with no children, the next snippet will fix the problem:

?
1
2
myButton.doubleClickEnabled = true ;
myButton.addEventListener( MouseEvent.DOUBLE_CLICK, myHandler );

What the heck is that? Well, apparently in order for a double click to fire, you have to enable it. Seems odd, I know, but I’m guessing it is a feature used to save a few cycles. I guess we can’t complain too much, at least we have double click now. For most cases, the last part would have fixed your issue. However, if you’re like me and your buttons are more complex with multiple children, you may still notice that this doesn’t work. This is where I struggled. The next part will finally fix the problem for all cases simple to complex:

?
1
2
3
myButton.mouseChildren = false ;
myButton.doubleClickEnabled = true ;
myButton.addEventListener( MouseEvent.DOUBLE_CLICK, myHandler );

Ok, so this is a little more obscure, but it does fix the problem. Now, let me explain what this is and why you need it. When a sprite contains children, those children will accept mouse input as well. Sometimes, the input will be registered to one of the children instead of the parent clip. So, when you attach a listener to the parent, and a child fires the event, you will never receive the event. The solution, set mouseChildren to false to tell the clip not to let any of its children accept mouse inputs. This way the parent clip will accept mouse input and fire the appropriate event.

Hopefully you found this before you spent as many hours as I did fumbling around.

 

 

你可能感兴趣的:(Blog,Flash,UP,Adobe)