tags:

views:

662

answers:

1

I have a flex component with 2 states - "" (ie no name / default) and "Transactional" - and a bunch of transitions to move from one state to the other.

There is a button to toggle between the two states which calls the following function when clicked

public function ToggleState():void
{
    if (this.currentState=="Transactional")
    {
        this.currentState = "";
    }
    else
    {
        this.currentState = "Transactional";
    }

}

Everything works as expected unless you click on the button whilst the component is changing from one state to the other. After that things start going strange - some components that would previously fade out, no longer disappear. Others no longer re-appear

I suspect this is because the transitions don't complete properly and so the properties of the components being animated are not properly reset to the correct values.

I tried to put in some checks to tell if the state is changing (and hence disable the button whilst the transitions are playing) but the only events I could find to listen for were

enterState
currentStateChange
currentStateChanging

all of which are fired before the transitions are complete.

Does anyone know of any other suitable events to listen for or a better way of doing the state change?

UPDATE:

Here is the mxml I am using for the transitions

<transitions>
    <mx:Transition fromState="" toState="Transactional">
        <mx:Sequence>
            <mx:Parallel>
                <mx:AnimateProperty target="{Controller}" property="y" fromValue="-60" toValue="-1" duration="600" />
                <mx:AnimateProperty target="{Environment}" property="y" fromValue="156" toValue="46" />
                <mx:AnimateProperty target="{ProfitAndLoss}" property="y" fromValue="156" toValue="126" />
                <mx:AnimateProperty target="{Summary}" property="y" fromValue="156" toValue="56" />
                <mx:AnimateProperty target="{Assets_Container}" property="x" fromValue="266" toValue="246" />
                <mx:AnimateProperty target="{Liabilities_Container}" property="x" fromValue="425" toValue="505" />
                <mx:Fade target="{TransactionalBackgroundImage}" alphaFrom="0" alphaTo="1" />
            </mx:Parallel>
            <mx:AnimateProperty target="{Summary}" property="x" fromValue="42" toValue="256" />
        </mx:Sequence>
    </mx:Transition>
    <mx:Transition fromState="Transactional" toState="">
        <mx:Sequence>
            <mx:AnimateProperty target="{Summary}" property="x" fromValue="256" toValue="42" />
            <mx:Parallel>
                <mx:AnimateProperty target="{Controller}" property="y" fromValue="-1" toValue="-60" />
                <mx:AnimateProperty target="{Environment}" property="y" toValue="156" fromValue="46" />
                <mx:AnimateProperty target="{ProfitAndLoss}" property="y" toValue="156" fromValue="126" />
                <mx:AnimateProperty target="{Summary}" property="y" toValue="156" fromValue="56" />
                <mx:AnimateProperty target="{Assets_Container}" property="x" fromValue="246" toValue="266" />
                <mx:AnimateProperty target="{Liabilities_Container}" property="x" fromValue="505" toValue="425" />
                <mx:Fade target="{TransactionalBackgroundImage}" alphaFrom="0" alphaTo="0" />
            </mx:Parallel>
        </mx:Sequence>
    </mx:Transition>
</transitions>
+1  A: 

The currentState property changes instantly, I'm pretty sure it's the transitions that cause the trouble (been there, done that ;-). If you click too fast, you have two concurrently running effects changing the same set of values, yielding an undefined result. Also, effectEnd event handlers on your effects will fire in the middle of another running effect. You might try the end() method on your effect instances to immediately end an effect before starting the next, this also sets all attributes to their final values and triggers effectEnd. If that does not help, can you post some of your effect code?

Simon
Hi, thanks for the response.I am defining the transitions in mxml not actionscript. I have posted the mxml for the transitions. I will try now to see if I can call the end() method on each of the effects before changing the state. Thanks
Addsy
That looks very clean to me, except for last Fade instance which does nothing: alphaFrom="0" alphaTo="0".
Simon
ah whoops! Thanks for pointing that out! Unfortunately the method you suggested meant that the transitions finished early - I tried it but the powers that be said that they needed to complete instead. You put me within spitting distance of the solution tho - There are other elements that simply disappear and appear when the state changes - ie no transition just setting visible to false. These properties are updated immediately so I could use them to test whether it was ok to do another state change. As I wouldn't have found it without your help I have accepted your answer. Many thanks
Addsy
With a little more effort, you can have complete transition effects: In ToggleState(), only change currentState if no effect is currently running (you can use Effect's isPlaying property for that). Otherwise, remember somewhere that the button was pressed, and check for that condition in an effectEnd handler for the transition effect. If there is a "pending click", change currentState there and remove the pending flag.
Simon