Search code examples
actionscript-3apache-flexflex4flex4.5

Flex How to change button state in actionscript?


i can do this:

<s:Button id="Btn" enabled.State1="false" />

But the following code is giving me an error.

 private function enableDisable():void{
       Btn.enabled.State1="false";  //Error: Access of undefined property State1
      }

how to code enabled.State1 in ActionScript?

Thanks


Solution

  • I know this is not what you want to hear, but here it goes anyway: why do you want to do that? The whole purpose of the states is that you wouldn't have to write tons of ActionScript to do the same thing.

    Why you can't do it like that

    By writing Btn.enabled.State1 in ActionScript you're essentially saying: give me the property called 'State1' of the Boolean instance called 'enabled'. Obviously that won't work because a Boolean doesn't have such a property. You're confusing the MXML dot (.) notation - used for assigning values to properties based on states - with the ActionScript dot notation - used for reading/writing properties.

    A solution or as close as it gets

    Since it's the very nature of this feature that you would use it in MXML, you can't do exactly what you're asking for in ActionScript. The next best thing would be to listen for StateChangeEvent en set the Button's 'enabled' property according to the new state name.

    addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE, onStateChange);
    
    private function onStateChange(event:StateChangeEvent):void {
        switch (event.newState) {
            case "wrong":   Btn.enabled = false; break;
            case "correct": Btn.enabled = true; break;
        }
    }
    

    (I'm using the same states as in James' answer)