feathersui / feathersui-starling

User interface components for Starling Framework and Adobe AIR
https://feathersui.com/learn/as3-starling/
Other
915 stars 386 forks source link

KeyToState: Null Object Reference when resetting button focus in KeyboardEvent.KEY_DOWN event handler #1762

Closed Adolio closed 5 years ago

Adolio commented 5 years ago

Hello Josh,

I have a tricky case where get a null object reference when I remove the focus of the triggered button directly in its trigger handler. Plus, the triggering of the button is happening by hand when SPACE key is pressed.

In a nutshell:

1) Call of FocusManager.focus = null; in a button event handler.

private function onButtonTriggered(e:Event):void 
{
    FocusManager.focus = null;
}

2) Call of the button trigger directly in Starling stage KeyboardEvent.KEY_DOWN event.

private function onKeyDown(e:KeyboardEvent):void 
{
    switch(e.keyCode)
    {
        // ...
        case userValidationKey: // Keyboard.SPACE
            if (FocusManager.focus is Button)
                (FocusManager.focus as Button).dispatchEventWith(Event.TRIGGERED);
            break;
        // ...
    }
}

Here is the stack trace:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at feathers.utils.keyboard::KeyToState/stage_keyDownHandler()[C:\Users\josht\Development\feathers\feathers\source\feathers\utils\keyboard\KeyToState.as:378]
    at starling.events::EventDispatcher/invokeEvent()[/Users/redge/Development/starling/starling/src/starling/events/EventDispatcher.as:159]
    at starling.events::EventDispatcher/dispatchEvent()[/Users/redge/Development/starling/starling/src/starling/events/EventDispatcher.as:130]
    at starling.display::DisplayObject/dispatchEvent()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObject.as:737]
    at starling.core::Starling/onKey()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:634]

Thanks in advance for your help.

joshtynjala commented 5 years ago

I've committed a change that should avoid this null object reference error.

However, I'm curious why you are dispatching Event.TRIGGERED manually on Keyboard.SPACE when the button has focus. You seem to be duplicating behavior that already exists in the Button class...

Adolio commented 5 years ago

Thanks Josh for the fix!

So basically what I'm trying to achieve is a Generic UI Navigation System which allows player/user to control & navigate the full UI with various input systems such as game-pad, keyboard or mouse.

My issue was that the user can configure the keyboard controls and map the SPACE action to the validation action. Then when the navigation system notices a SPACE key down, it fakes a button trigger and Feathers does it as well which causes the above issue.

In a nutshell the UI Navigation System allows to:

This feature is currently in standby because it requires to dig a bit too much in Feathers to solve tricky cases but if you are interested to have a look I can share what I have so far, feel free to ask. Otherwise I will perhaps come back to you later on if I'm facing something I cannot solve by myself.