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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值