- Requirements
Prerequisite knowledge Required products Sample files
Flash Builder 4.7 Premium (Download trial)
User level    
Beginning    

 

In this exercise you will use effects to animate components as they transition between states of an application.
 
In the Employee Portal example, Panel containers in the login and portal states will move and fade in and out as you switch states. You will also use composite effects to control which elements move in parallel or sequentially. Lastly, you will learn how to explicitly control the appearance and disappearance of components during the transition that exist in one state but not the other.
 
Review your task for this exercise.
Figure 1. Review your task for this exercise.
In this exercise, you will learn how to:
 

 
Instantiate transitions

In this section you will animate the Employee of the Month Panel container as it transitions between two applications states.
 
  1. Download the ex4_10_starter.zip file if you haven't already and extract the ex4_10_starter.fxp file to your computer.
  2. Open Flash Builder.
  3. Import the ex4_10_starter.fxp file.
  4. Open the ex4_10_starter.mxml file.
  5. Locate the states block.
<s:states> <s:State name="portalState"/> <s:State name="loginState"/> </s:states>
    Remember that the Employee Portal application has two distinct states that you defined in earlier exercises. Next, you will define the animation that occurs on the Employee of the Month Panel container when the application transitions from the login to the portal state.
     
  1. Below the closing states tag, create the transitions property of the application.
  2. Within the transitions block, create a Transition tag block.
<s:transitions> <s:Transition> </s:Transition> <s:transitions>
  1. To the opening Transition tag, add the fromState property with a value of loginState and the toState property with a value of portalState.
<s:Transition fromState="loginState" toState="portalState"> </s:Transition>
This is a one-way transition. You will create an animation for the reverse transition later in this exercise.
 
  1. Between the Transition tags, create a Parallel effect tag block.
<s:Transition fromState="loginState" toState="portalState"> <s:Parallel> </s:Parallel> </s:Transition>
In the following steps you will apply both a Fade and a Move effect to the Employee of the Month Panel container. The effects will happen at the same time, which is why you are using a Parallel composite effect instead of a Sequence effect.
 
  1. To the Parallel effect, assign the target property value to the employeeOfTheMonth Panel container.
<s:Parallel target="{employeeOfTheMonth}"> </s:Parallel>
  1. Within the Parallel effect tags, add a Fade effect instance.
<s:Parallel target="{employeeOfTheMonth}"> <s:Fade/> </s:Parallel>
  1. Below the Fade effect, add a Move effect.
<s:Parallel target="{employeeOfTheMonth}"> <s:Fade/> <s:Move/> </s:Parallel>
  1. Save the file.
  2. Run the application and log in with the username, flex, and the password,hero.
You should see the login Panel container disappear, the employeeOfTheMonth Panel container fade into view while moving into position, and the other Panel containers in the portalState just appear (see Figure 2).
 
See the Panel container fades and moves into place.
Figure 2. See the Panel container fades and moves into place.

 
Transition multiple controls

In this section you will animate multiple containers so that they move into place from the locations shown in Figure 3.
 
See how the Panel containers are positioned before the animation effects are processed.
Figure 3. See how the Panel containers are positioned before the animation effects are processed.
  1. Return to Flash Builder.
  2. To the Parallel effect, convert the target property to the targets property.
<s:Parallel targets="{employeeOfTheMonth}"> ... </s:Parallel>
You want all the Panel instances to animate at the same time.
 
  1. Assign the targets property value to the four Panel instances that appear in the portalState. Remember to add the array syntax square brackets inside the curly braces of the binding.
<s:Parallel targets="{[employeeOfTheMonth, search, cafeteriaSpecial, monthlyEvents]}"> ... </s:Parallel>
  1. To the nested Move effect, assign the target property value to the employeeOfTheMonth Panel container.
<s:Parallel targets="{[employeeOfTheMonth, search, cafeteriaSpecial, monthlyEvents]}"> <s:Fade/> <s:Move target="{employeeOfTheMonth}"/> </s:Parallel>
You want all of the Panel instances to fade in except for the employeeOfTheMonth panel, which already appears in both states. By simply leaving the Fade effect without any properties, the application will automatically determine the alpha property of each target component and extrapolate from there how to make it fade from one state to the other.
 
Each Panel instance will move in a different way. Since the employeeOfTheMonth instance already has a starting point in the loginState of the application, you simply left the Move effect without properties. Again, the application will automatically determine how to animate the x and y properties of the employeeOfTheMonth instance to make it move to the proper location in the portalState from its initial position in the loginState.
 
The other three Panel container instances that are referenced in the targets property of the Parallel effect do not exist in the loginState and they currently just appear in their final position in the portalState. In the next steps, you will define a starting position for each of the instances using either the xFrom or yFrom properties of the Move effect.
 
Figure 3 above gives a practical illustration of where the Panel instances will start from when the animation begins.
 
  1. Below the Move effect for the employeeOfTheMonth instance, add another Move effect instance.
  2. To the second Move effect, assign the target property value to the search Panel container and the xFrom property value to -166.
<s:Move target="{employeeOfTheMonth}"/> <s:Move target="{search}" xFrom="-166"/>
  1. Below the second Move effect, add a third Move effect and assign the target property value to the cafeteriaSpecial Panel container and the yFrom property value to -329.
<s:Move target="{search}" xFrom="-166"/> <s:Move target="{cafeteriaSpecial}" yFrom="-329" />
  1. Below the third Move effect, add a fourth Move effect and assign the target property value to the monthlyEvents Panel container and the xFrom property to 833.
<s:Move target="{cafeteriaSpecial}" yFrom="-329" /> <s:Move target="{monthlyEvents}" xFrom="833" />
  1. Save the file and run the application.
  2. Enter flex for the username and hero for the password, and then click the Submit button.
  3. You should see the login panel disappear and the panels within the portal state move into place from their off-screen locations as shown in Figure 4.
     
Run the application and log in to see the Panel containers animate into place.
Figure 4. Run the application and log in to see the Panel containers animate into place.

 
Nest composite effects

In this section you will use a Sequence effect to define the order in which the panels will animate as they transition between the login and portal states. In the last section you had all the panels in the portal state fade and move into place in parallel. But before that happens, you need the login Panel instance to fade and move out of view.
 
  1. Return to Flash Builder.
  2. Surround the Parallel effect with a Sequence effect tag block.
<s:Sequence> <s:Parallel target="{[employeeOfTheMonth, search, cafeteriaSpecial, monthlyEvents]}"> ... </s:Parallel> </s:Sequence>
  1. Between the opening Sequence tag and the Parallel effect, create another Parallel effect instance.
<s:Sequence> <s:Parallel> </s:Parallel> <s:Parallel target="{[employeeOfTheMonth, search, cafeteriaSpecial, monthlyEvents]}"> ... </s:Parallel> </s:Sequence>
  1. Within the new Parallel effect tags, add a Fade effect and assign the target property value to the login Panel container.
<s:Parallel> <s:Fade target="{login}"/> </s:Parallel>
You could have put the target property on the Parallel effect instead of on the nested Fade effect. However, we will next add more effects inside of this composite effect that target another component.
 
  1. Under the Fade effect, add a Move effect and assign the target property value to the login Panel container and add the xTo property with a value of -266.
<s:Parallel> <s:Fade target="{login}"/> <s:Move target="{login}" xTo="-266"/> </s:Parallel>
  1. Below the Move effect, add another Move effect and assign the target property value to the employeeOfTheMonth Panel container.
<s:Fade target="{login}"/> <s:Move target="{login}" xTo="-266"/> <s:Move target="{employeeOfTheMonth}"/>
  1. From the second Parallel effect block, remove the employeeOfTheMonth Panel container from the targets property value and the corresponding Move effect.
<s:Parallel targets="{[search, cafeteriaSpecial, monthlyEvents]}"> <s:Fade/> <s:Move target="{search}" xFrom="-166"/> <s:Move target="{cafeteriaSpecial}" yFrom="-329" /> <s:Move target="{monthlyEvents}" xFrom="833" /> </s:Parallel>
The changes you just made will fade and move the login panel out while the employeeOfTheMonth panel moves into its new location in the portalState. Only after that happens, will the second set of effects run, which animate the rest of the portalState panels into place.
 
  1. Save the file and run the application.
  2. Enter flex for the username and hero for the password, and then click the Submit button.
You should see the animation is not correct. The Panel containers within the portalState state are visible in their final locations before the animations occur.
 

 
Add and remove components during the transition

You have seen that the application can usually determine how to animate an effect on a component based on the beginning and ending properties that it is modifying. However, as an animation becomes more complex, the Flex framework needs more direction for how to animate elements that exist in only one state of the transition.
 
In this section, you will use the AddAction and RemoveAction effects to explicitly define when a component is added or removed during the transition between the two states.
 
  1. Return to Flash Builder.
  2. Within the Sequence tag, between the Parallel effects, add the RemoveAction tag with a target property bound to the login instance.
<s:Parallel> ... </s:Parallel> <s:RemoveAction target="{login}"/> <s:Parallel targets="{[search, cafeteriaSpecial, monthlyEvents]}"> ... </s:Parallel>
You have just told the application to first animate the login and employeeOfTheMonth instances and then remove the login instance, which does not exist in the portal state.
 
  1. Within the second set of Parallel tags, add the AddAction tag as the first effect.
<s:Parallel targets="{[search, cafeteriaSpecial, monthlyEvents]}"> <s:AddAction/> <s:Fade/> ...
You have now told the application to explicitly add the panel instances to the transition at this point before you fade and move them.
 
  1. Save the file and run the application.
  2. Enter flex for the username and hero for the password, then click the Submit button.
You should see the Panel containers except for the Employee of the Month panel animate properly upon switching to the portalState state, as shown in Figure 5.
 
Log in and see the Panel containers except for the Employee of the Month, animate correctly.
Figure 5. Log in and see the Panel containers except for the Employee of the Month, animate correctly.

 
Use the Resize effect

In this section you will use the Resize effect to change the size of the employeeOfTheMonth Panel container between states.
 
  1. Return to Flash Builder.
  2. Locate the first Parallel effect tag.
  3. To the second nested Move effect, add the xFrom property with a value of 298 and the xTo property value of 24. These properties values represent the x position of the employeeOfTheMonth component in each application state.
<s:Move target="{login}" xTo="-266"/> <s:Move target="{employeeOfTheMonth}" xFrom="298" xTo="24"/>
  1. Between the Fade and the Move effects, add a Resize effect and assign the target property value to employeeOfTheMonth, the widthFrom property value to 390 and the widthTo property value to 250.
<s:Fade target="{login}"/> <s:Resize target="{employeeOfTheMonth}" widthFrom="390" widthTo="250"/> <s:Move target="{login}" xTo="-266"/> <s:Move target="{employeeOfTheMonth}" xFrom="298" xTo="24"/>
  1. Save the file and run the application.
  2. Enter flex for the username and hero for the password, then click the Submit button.
You should see the Employee of the Month panel animate before the other Panel containers within the portal state (see Figure 6).
 
Log in and see the Employee of the Month Panel container animates first.
Figure 6. Log in and see the Employee of the Month Panel container animates first.
Now you will add the autoReverse property to the transition so that your application animates when you transition from the portalState back to the loginState.
 
  1. Return to the ex4_10_starter.mxml file.
  2. To the opening Transition tag, add the autoReverse property with a value of true.
  3. Save the file and run the application.
  4. Enter flex for the username and hero for the password, then click the Submit button.
  5. Click the Logout button.
    Notice that the transition reverses, however, it does not animate correctly. In the next section you will add a second transition from the portalState to the loginState.
     
  6. Return to Flash Builder.
  7. From the opening Transition tag, remove the autoReverse property.
  8. Save the file.

 
Create a logout transition

In this section you will create a second transition to animate the components when the user logs out of the application. When this happens, you will animate from the portal state back to the login state.
 
  1. Return to Flash Builder.
  2. Within the transitions tags, below the existing Transition block, add another Transition tag block.
<s:Transition fromState="loginState" toState="portalState"> ... </s:Transition> <s:Transition> </s:Transition>
  1. To the new Transition instance, add the fromState property with a value of portalState and the toState property with a value of loginState.
<s:Transition fromState="portalState" toState="loginState"> </s:Transition>
When the user logs out of the Employee Portal application, you will fade out the search, cafeteriaSpecial, and monthlyEvents panels while the employeeOfTheMonth instance resizes and moves to its proper position in the login state. The next composite effect that you want to animate when that is done is for the Login panel to fade and move in from the left of the screen.
 
  1. Within the Transition effect, nest a Sequence effect.
<s:Transition fromState="portalState" toState="loginState"> <s:Sequence> </s:Sequence> </s:Transition>
  1. Within the Sequence tags, add a Parallel effect to animate out the panels in the portalState.
<s:Sequence> <s:Parallel> </s:Parallel> </s:Sequence>
  1. Within the Parallel effect add a Fade effect and assign the targets property value to the three Panel containers that exist only in the portalState state.
<s:Parallel> <s:Fade targets="{[search, cafeteriaSpecial, monthlyEvents]}"/> </s:Parallel>
  1. Below the Fade effect, add a Resize effect and assign the target property value to the employeeOfTheMonth Panel container, the widthFrom property value to 250 and the widthTo property value to 390.
<s:Fade targets="{[search, cafeteriaSpecial, monthlyEvents]}"/> <s:Resize target="{employeeOfTheMonth}" widthFrom="250" widthTo="390"/>
  1. Below the Resize effect, add the Move effect and assign the target property value to the employeeOfTheMonth Panel container, the xFrom property value to 24 and the xTo property value to 434.
<s:Resize target="{employeeOfTheMonth}" widthFrom="177" widthTo="264"/> <s:Move target="{employeeOfTheMonth}" xFrom="24" xTo="434"/>
  1. Below the Parallel composite effect, but still inside the Sequence effect, create a second set of Parallel tags that target the login instance.
<s:Sequence> <s:Parallel> ... </s:Parallel> <s:Parallel target="{login}"> </s:Parallel> </s:Sequence>
  1. Within the new Parallel effect, add a Fade effect.
<s:Parallel target="{login}"> <s:Fade/> </s:Parallel>
In the previous steps, you have defined the animations that should occur in sequence. First, all the panels in the portal state will fade, except the Employee of the Month panel, which will move to its new location. Then the Login panel will fade and move into place in the login state.
 
Lastly you need to define when the panels that do not exist in both states need to be added or removed during the transition.
 
  1. Between the two Parallel tag blocks, add the RemoveAction tag and assign the targets property value to the Panel containers that exist only in the portalState state.
<s:Sequence> <s:Parallel> ... </s:Parallel> <s:RemoveAction targets="{[search, cafeteriaSpecial, monthlyEvents]}"/> <s:Parallel target="{login}"> ... </s:Parallel> </s:Sequence>
  1. Still between the Parallel tag blocks, but below the RemoveAction effect, add an AddAction tag and assign the target property value to the login Panel container.
<s:Sequence> <s:Parallel> ... </s:Parallel> <s:RemoveAction targets="{[search, cafeteriaSpecial, monthlyEvents]}"/> <s:AddAction target="{login}"/> <s:Parallel target="{login}"> ... </s:Parallel> </s:Sequence>
  1. Save the file and run the application.
  2. Enter flex for the username and hero for the password, and then click the Submit button.
  3. Click the Logout button.
  4. You should see that the Employee of the Month panel moves and resizes while the other panels fade away, and then the Login panel fades into view and moves into place as shown in Figure 7.
     
Log out to see the Login and Employee of the Month panels animate.
Figure 7. Log out to see the Login and Employee of the Month panels animate.
In this exercise you used effects to animate components as they transition between states of an application.
 
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.