O'Reilly logo

Programming Flex 2 by Joey Lott, Chafic Kazoun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using ActionScript to Define States

In most cases, it's more appropriate to use MXML than ActionScript to define states. However, sometimes you want to define states dynamically (in which case, you must use ActionScript). One such example is when you want to define a multipage form based on data loaded at runtime. Creating the form dynamically is advantageous because you can change the form without recompiling and republishing the SWF. However, because form elements aren't known at compile time, you cannot use MXML to define the states.

The ActionScript used to work with states corresponds to the MXML. In the following sections, you'll learn about the ActionScript equivalents to the MXML you learned about earlier.

Defining States

When defining states using ActionScript, you use the mx.states.State class. You can use the constructor as part of a new statement to define a new State instance:

var stateA:State = new State(  );

You can assign a name to a state using the name property, in much the same way as you'd use the name attribute of the <mx:State> tag:

stateA.name = "exampleStateA";

And just as you can use the basedOn attribute of the <mx:State> tag to define state inheritance, you can use the basedOn property of the State class. The following code defines a new state that is based on the state constructed in the previous two code snippets. Note that the basedOn property expects a string specifying the name of the state upon which you want to base the new state; you cannot assign it a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required