MOUSE_OUT events not firing on child when parent i

2019-08-28 00:28发布

问题:

I make a sprite, then add a child sprite.

I add a listener to the child for MOUSE_OUT events.

If my mouse is in the child sprite when I set the parent sprite mouseEnabled=false and mouseChildren=false, MOUSE_OUT is not fired on the child.

But then, when I move the mouse, MOUSE_OUT is fired on the child. MOUSE_OUT is also fired if I click. MOUSE_OUT is not fired if I mousewheel.

So, what's going on here?

This is a related question.


After studying back2dos' code, I discovered what I am doing differently is calling stage.focus = null just before setting mouseChildren = mouseEnabled = false. I am setting the stage focus to null to clear the blinking cursor from a textfield... is there a better way to do this?


here is back2dos' modified code. steps to reproduce: click the textfield, get the blinking cursor. click the "button", but don't move the mouse between down and up. note the cursor is not blinking (good). also note that the mouse_out events on the button have not fired. until you move or click the mouse.

package  {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.ColorTransform;
    public class Test extends Sprite {
        private var child:Sprite
        public function Test() {
                this.addChild(child = new Sprite());
                child.graphics.beginFill(0xFF0000);
                child.graphics.drawRect(0, 0, 100, 20);
                child.addEventListener(MouseEvent.CLICK, onClick);
                child.addEventListener(MouseEvent.MOUSE_OUT, onOut);
                child.addEventListener(MouseEvent.MOUSE_OVER, onOver);


        tf = new TextField( );
        tf.backgroundColor = 0xFF00AA;
        tf.background = true;
        tf.type = TextFieldType.INPUT;
        tf.text = "yo";
        tf.y = 100;
        this.addChild( tf );
        }
        private function onOver(e:MouseEvent):void {
                child.transform.colorTransform = new ColorTransform(0, 0, 0, 1, 0, 0xFF);
        }
        private function onOut(e:MouseEvent):void {
                child.transform.colorTransform = new ColorTransform();
        }
        private function onClick(e:MouseEvent):void {
            //try it here...
            stage.focus = null;
                this.mouseChildren = this.mouseEnabled = false;
                //or try it here...
                //stage.focus = null;
        }

    }
}

回答1:

there must be something wrong with your code ... i have a minimum setup here:

package  {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.ColorTransform;
    public class Test extends Sprite {
        private var child:Sprite
        public function Test() {
            this.addChild(child = new Sprite());
            child.graphics.beginFill(0xFF0000);
            child.graphics.drawRect(0, 0, 100, 20);
            child.addEventListener(MouseEvent.CLICK, onClick);
            child.addEventListener(MouseEvent.MOUSE_OUT, onOut);
            child.addEventListener(MouseEvent.MOUSE_OVER, onOver);
        }
        private function onOver(e:MouseEvent):void {
            child.transform.colorTransform = new ColorTransform(0, 0, 0, 1, 0, 0xFF);
        }
        private function onOut(e:MouseEvent):void {
            child.transform.colorTransform = new ColorTransform();
        }
        private function onClick(e:MouseEvent):void {
            this.mouseChildren = this.mouseEnabled = false;
        }
    }
}

should turn green, when you mouse over, and red again, if you mouse out ... on click, it is disabled with this.mouseChildren = this.mouseEnabled = false ... on my machine, this triggers mouseOut (so the rectangle turns red again) ... this makes sense to me ... and the thing with getting mouse outs when clicking, is a definite indicator to me, you must be having a bug somewhere ... could you try to reduce the problem and post it?

greetz

back2dos



回答2:

try MouseEvent.ROLL_OVER instead