2 May 2011
Beginning
In this exercise, you will use Spark effects to animate a user login panel when invalid information is submitted (see Figure 1). When users click the Login button in the form with the wrong authentication information, the Login panel will shake horizontally.
In this exercise, you will learn how to:
In this section you will start the Panel container's shaking effect by defining that the panel should move 20 pixels to the right.
You should see the Employee Portal login screen shown in Figure 2.
You should see the Employee Portal main page shown in Figure 3.
Declarations comment.Declarations block.<!-- Declarations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Declarations>
</fx:Declarations>
Declarations block, create an instance of the Spark Move effect.<fx:Declarations>
<s:Move/>
</fx:Declarations>
Move effect, add the id property with a value of shake.<fx:Declarations>
<s:Move id="shake"/>
</fx:Declarations>
Move effect, bind the target property value to the login Panel container and assign the xBy property a value of 20 pixels.<fx:Declarations>
<s:Move id="shake" target="{login}" xBy="20"/>
</fx:Declarations>
In this section, you will handle the login functionality. If the user authenticates, then you will simply switch application views and display the main portal state. If the user fails authentication, then you will play the negative shake effect.
Script comment.Script block.<!-- Script
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Script>
<![CDATA[
]]>
</fx:Script>
Script block, create a private function named checkLogin() that returns a void return type.<fx:Script>
<![CDATA[
private function checkLogin():void
{
}
]]>
</fx:Script>
username TextInput control for the string flex and the password TextInput control for the characters hero.private function checkLogin():void
{
if (username.text == "flex" && password.text == "hero")
{
}
}
If the user property authenticates, you will switch the application state. Remember that you switch states using the currentState property of the component, which is in this case the main Application container.
currentState property a value of portalState.private function checkLogin():void
{
if (username.text == "flex" && password.text == "hero")
{
currentState='portalState';
}
}
checkLogin() function, below the closing brace of the conditional statement, add an else statement.private function checkLogin():void
{
if (username.text == "flex" && password.text == "hero")
{
currentState='portalState';
}
else
{
}
}
play() method of the shake effect instance.private function checkLogin():void
{
if (username.text == 'flex4' && password.text == "gumbo")
{
currentState='portalState';
}
else
{
shake.play();
}
}
Button control nested within the Login Panel container.Button control, assign the checkLogin() function as the event handler for the click event.<s:Button label="Submit"
click="checkLogin()"/>
Add the displayAsPassword property and set the value to true.
By adding this property, you will specify whether the text field is a password text field. If set to true, the input characters are hidden with asterisks.
<s:TextInput id="password"
displayAsPassword="true"/>
You should see that the Login Panel container moves 20 pixels to the right towards the other Panel container (see Figure 4).
You should see the application change to the Portal.
In this section you will use the Sequence effect to create a shaking motion when an invalid entry is submitted to the login Panel container.
Declarations comment, surround the Move effect with a composite Sequence effect.<s:Sequence>
<s:Move id="shake" target="{login}" xBy="20"/>
</s:Sequence>
Declarations block, cut the id and target properties from the Move effect and paste them into the Sequence effect.<s:Sequence id="shake" target="{login}">
<s:Move xBy="20"/>
</s:Sequence>
By placing the id and target on the Sequence composite effect, all of the effects nested within will be run at the same time.
Sequence block, copy the Move effect and paste seven copies below the first.<s:Move xBy="20"/>
<s:Move xBy="20"/>
<s:Move xBy="20"/>
<s:Move xBy="20"/>
<s:Move xBy="20"/>
<s:Move xBy="20"/>
<s:Move xBy="20"/>
<s:Move xBy="20"/>
Move effect, change the xBy property value to -20.<s:Move xBy="20"/>
<s:Move xBy="-20"/>
<s:Move xBy="20"/>
<s:Move xBy="-20"/>
<s:Move xBy="20"/>
<s:Move xBy="-20"/>
<s:Move xBy="20"/>
<s:Move xBy="-20"/>
You should see the Login Panel container move back and forth in a shaking motion, but notice that the panel shakes rather slowly.
In this section you will use the duration property of the effect to speed up the rate at which the Login panel shakes. Doing this will give the animation a more emphatic gesture.
duration property with a value of 20 milliseconds to the Sequence effect instance. <s:Sequence id="shake"
target="{login}"
duration="20">
<s:Move xBy="20" />
<s:Move xBy="-20" />
<s:Move xBy="20" />
<s:Move xBy="-20" />
<s:Move xBy="20" />
<s:Move xBy="-20" />
<s:Move xBy="20" />
<s:Move xBy="-20" />
</s:Sequence>
You should see that the Login Panel container shakes more quickly now. Notice that if you continually press the Submit button, the Login Panel container moves to the right. This happens because you are executing the shake.play() function while the function is already playing.
checkLogin() function in the Script block.else statement to check if the animation is playing by adding an if statement to evaluate !shake.isPlaying. You will execute the shake.play() function only if the animation is not already running.else
{
if(!shake.isPlaying)
{
shake.play();
}
}
You should see that the Login Panel container shakes, but it does not move to the right.
In this exercise you used Spark effects to animate a user login panel when invalid information is submitted. In the next exercise will use 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.