Skip to main content
Known Participant
July 24, 2013
Question

Adobe AIR bug with focus

  • July 24, 2013
  • 1 reply
  • 1702 views

Hi,

I found a bug in AIR where keyboard events are not fired after a click on a removed button

This test app reproduce the bug :

- add keyboard listeners to your stage

- add a container (a Sprite) to the stage

- add a button (for example a Sprite with buttonMode=true) to the container

- on click on the button, remove the container from the stage

now if you launch the test app, you're keyboard events are correctly fired until you click on the button.

after clicking on the button, keyboard events are not fired because the parent of the button has been removed from stage

note that if you does'nt set buttonMode=true on the button, the bug doesn't appear...

this is the code I used to produce the bug :

public class test extends Sprite

    {

        private var container:Sprite;

        public function test()

        {

            container = new Sprite();

            addChild(container);

            var rectangleBleu:Shape = new Shape();

            rectangleBleu.graphics.beginFill(0x0000FF);

            rectangleBleu.graphics.drawRect(50, 30, 100, 30);

            var button:Sprite = new Sprite();

            button.addChild(rectangleBleu);

            button.x = 200;

            button.y = 200;

            button.buttonMode = true;

            container.addChild(button);

            button.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownSprite);

            button.addEventListener(MouseEvent.CLICK, onClickSprite);

            container.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

            container.stage.focus = stage;

            stage.addEventListener(FocusEvent.FOCUS_IN, onFocus);

            stage.addEventListener(FocusEvent.FOCUS_OUT, onFocus);

            stage.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, onFocus);

            stage.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, onFocus);

            trace(stage.focus);

        }

        private function onMouseDownSprite(e:MouseEvent):void {

            trace("mouse_down");

            trace(stage.focus);

        }

        private function onClickSprite(e:MouseEvent):void {

            trace("click");

            removeChild(container);

            trace(stage.focus);

        }

        private function onKeyDown(e:KeyboardEvent):void {

            trace("key: "+e.keyCode);

            trace(stage.focus);

        }

        private function onFocus(e:FocusEvent):void {

            trace(e);

        }

    }

This bug is very annoying because I'm writting a real time AS3 game, and if Keyboard Events are not fired, the player is blocked in a direction...

Thanks

This topic has been closed for replies.

1 reply

Participating Frequently
July 25, 2013

when  you remove the  container ,  The  container.stage == null. 

before you  say  "that is a bug" ,PLZ REVIEW YOUR CODE AT LEAST 999 TIMES.

PhendraxAuthor
Known Participant
July 26, 2013

Hello,

I think you didn't read the full post and didn't test the code

the problem doesn't appears when you remove from stage the object which have the focus

the problem only appears when you remove the parent of the object which have the focus AND if this object has buttonMode=true

PLZ READ BEFORE TROLLING

bref,

Note that the FocusEvent doesn't help here because no FocusEvent is dispatched when removing the parent of the focused object : stage.focus is still this object, but stage.focus.stage is null (cause it's removed, and I think that's why the stage doesn't fire the KeyboardEvents)

Participating Frequently
July 26, 2013

yes ,i'm  not test the code.

1\ for me ,i will not proceed keyboardEvent like you .

2\  you  write code like  object.parent.stage.addEventxxxx (point to  container.stage.addEventXXX)


the problem doesn't appears when you remove from stage the object which have the focus  ---------------  the container still have stage Property ,and  is not null.

the problem only appears when you remove the parent of the object which have the focus AND if this object has buttonMode=true --------------  you remove the  container of  object .so,  container.stage is null .

Raymond