Question

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

Was it helpful?

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)

OTHER TIPS

I think you may be using states in the wrong context. For instance, you have component which contains a user input with a button next to it. The button is only enabled when the correct word is input. You would define two states for the component, perhaps correct and wrong.

<s:states>
    <s:State name="wrong" />
    <s:State name="correct" />
<s:states>

You would then, similar to what you've done above, set individual properties for the buttons depending on the state:

<s:Button id="Btn" enabled.wrong="false" enabled.correct="true" />

By default, the state of the component would be wrong. After handling user input and checking if the correct word is entered, the state of the component would be changed to correct.

Normally the state-specific properties of components are set at compile time and the state of the component itself changed at runtime.

Here is an overview of states in Flex 4.6

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top