MouseChildren = false doesn't work very well for me

Happy New Year, by the way!

I want to separate the event handling from the container and its child. So, as you can see, my source code is very simple:

package { import flash.display.Sprite; import flash.display.*; import flash.events.*; public class test extends Sprite{ public function test() { var container:Sprite = new Sprite(); // my container container.graphics.beginFill(0, 1); // whatever the color container.graphics.drawRect(0, 0, 100, 100); // origin at 0,0 container.graphics.endFill(); addChild(container); var decor:Sprite = new Sprite(); // and it child decor.graphics.beginFill(0, 1); // whatever the color decor.graphics.drawRect(200, 200, 100, 100); // origin at 200,200 decor.graphics.endFill(); container.addChild(decor); container.mouseChildren = false; container.addEventListener(MouseEvent.ROLL_OVER, onOver, false, 0, true); } private function onOver(e: MouseEvent):void { trace("ROLL trace"); } } } 

When I look at the container object, I have a trace (OK for me). BUT When I look at the decor object, I also have a trace (not what I want). I just want the container to be fired by a mouse event, not a child. So what happened to my mouseChildren = false ....? I do not understand...

+4
source share
2 answers

The decor object is a member of container , and therefore it is evaluated along with any other content within the container .

mouseChildren = false; is not a way to completely disable mouse events, but to reduce complexity in composite display objects: the mouse event is still triggered, but the event target property will not contain a link to the child object. actually turned upside down, but only for the parent on which the property was set.

If you want to completely ignore decor , use decor.mouseEnabled = false; .

+4
source

I tried mouseEnabled = false and it doesn't work either. In another forum, the guys told me that a 'populated object in a container will call an event handler . Therefore, his solution is to have a container and create 2 children: one handles the mouse event, and the other as a decor.

And it works very well.

+1
source

Source: https://habr.com/ru/post/1389003/


All Articles