I like the simple things. Like Cheerios cereal, one-click-checkout and Flex 4 states. If your only experience with states is with Flex 2/3 then I’ll understand your confusion. But Flex 4 states are a completely different animal than it’s predecessors. Here’s a painfully simple example.
| view source |
Take a look at the code:
<s:states>
<s:State name="x_mark" />
<s:State name="check_mark" />
</s:states>
<s:transitions>
<!--To: check-mark -->
<s:Transition toState="check_mark" autoReverse="true">
<s:Fade targets="{ [ xMark, checkMark ] }" duration="500"/>
</s:Transition>
<!--To: X-mark -->
<s:Transition toState="x_mark" autoReverse="true">
<s:Fade targets="{ [ xMark, checkMark ] }" duration="500"/>
</s:Transition>
</s:transitions>
<s:BitmapImage
id="checkMark"
source="assets/icons/check_mark.png"
width="20" height="20"
includeIn="check_mark"/>
<s:BitmapImage
id="xMark"
source="assets/icons/x_mark.png"
width="20" height="20"
includeIn="x_mark"/>
Notice in the <Fade> I don’t say anything about fade to alpha=”1″ or alpha=”0″. That’s because the <Fade> assumes that a target that exists but is going to be removed can fade to alpha=”0″ without being told to. And it knows that if a target doesn’t exist and is being added then it will fade to alpha=”1″. That’s going to be the case in the overwhelming majority of cases. So it just does it. No fuss. No muss. :)
I still wouldn’t need to modify the transitions if I wanted to fade to values other than the defaults. I would just use the new property.state syntax like this:
<s:BitmapImage id="checkMark" source="assets/icons/check_mark.png" width="20" height="20" alpha.check_mark="0.5" includeIn="check_mark"/>
Here I’m just telling the bitmapImage to have an alpha of 0.5 when in the check_mark state. The transition will pick this up and now fade to 0.5 instead of 1.0. No need to go and edit the transition at all.
In fact it’s a bad idea to add specific values to a transition unless you absolutely need to. All the transition should do is describe what states to transition to, the duration, and the effect to use – move, fade, scale, et al. If you do it that way then it’s simple to edit the transition by just coming back to your component and changing the values.
Also try and avoid using the fromState in your <transition>. Most of the time you won’t care what state you’re coming from. It’s the state your going to that matters.
Lastly, notice the autoReverse=”true” parameter. That enables the transition to be able to back-up without finishing the transition. You can see that if you click on the label/checkmark quickly. It will only fade part of the way and then back-up. No herky-jerky transitions.
I don’t know if it could be any easier than this.





Good post. For this example you could make the transition even simpler:
<s:transitions>
<s:Transition>
<s:Fade duration="500" targets="{[xMark, checkMark]}"/>
</s:Transition>
</s:transitions>
The Fade will then figure out what the transition should be between each combination of states for you.
You could also remove the 2 BitmapImages and just change the source depending on the state
<s:BitmapImage
id="bitmapImage"
source="assets/icons/x_mark.png"
source.check_mark="assets/icons/check_mark.png"
width="20" height="20"/>
@Tink: I don’t think the fade would work if the source were changing. But if there were no need to fade then that would work fine. It’s going to take some time to really get our heads around the power of this now states syntax. It just occurred to me the other day that it also applies to events.
Also you sure this is a working example?
As far as I understand BitmapImage is not capable of loading an external file, as in your example.
@Tink: is the new . The code is running in the example.
You could sequence 2 Fades.
"is the new"?
loading doesn't work for me although I'm not surprised as when I look inside the version of BitmapImage I have, theres nothing to load anything.
http://flexponential.com/2010/03/17/using-non-embedded-images-in-a-spark-bitmapimage/
@Tink: @Steven pointed out that loading was removed post beta 2. I haven’t updated so that’s why.
Support for loading external images in BitmapImage was removed post beta2 (see svn 11376). If you are on a build older than this then that might explain why it is working for you.
the new states syntax is powerful and intuitive……perhaps its out there but I couldn't find it….is there any way to use the new state syntax with actionscript only components? you can define states, add them to a state array, assign the array to the states property of the (root) component, but the defined states are not known by any of the child components in the root component so trying to syntax, for example, button.state1.label = "I'm in state1" throws and "undefined property" error.
@DannyV, That’s a great question. To my knowledge it isn’t possible to use the new states syntax with AS only components yet because there is no way to dynamically add states. They can only be added using MXML. However, the Flex team has said that they are adding the ability to make states dynamic. Hopefully with the first update. I know I’ve been waiting for it.