This
month's download is a a ZIP file containing the AS 2.0
state engine and tools, an example TestCases file to show
how to use the engine, and the book's Stolex watch implemented
using that code.
Sign
up now and get access to download the code through the
subscriber's edition! |
We have completed our
port of the original Flash MX state engine (FStEng v 1.5) to AS
2.0, and added some tools into the distribution for helping to build
programs using the state engine. This package can be downloaded
by clicking on the link in the box on the right.
Once you have installed
the MXP using the Macromedia Extensions Manager, you are ready to
go. Any code you have written for the Flash MX version will work
identically in the Flash MX 2004 provided that you add the following
line to the top of the script in which you build your state machines:
import mx.fmxis.FStEng.OneFive.*;
|
The API is identical
to the API for our Flash MX version. The main difference between
these versions is that the Flash MX 2004 version does not have a
component tile that you drag into your current movie--you simply
start declaring your state engine (stateEngine),
and Flash automatically pulls in the code from its installed location
within the Flash install directory, inside Classes
> mx > fmxis. The statement above tells Flash to look
in this folder for the class definitions.
Our AS 1.0 components
do not work with AS 2.0, so we
have updated all of our components (you'll see a couple of them
in the downloadable Stolex watch example files). With the book's
components, we hardcoded communication with an ieh()
method, since we used the components with our engine's internal
event handler (ieh). In our AS 2.0 components, however, we used
Macromedia's approach to events/listeners, in which you can subscribe
to individual events or define a method handleEvent()
to catch all events. Also, the parameter passed to an event handler
is now an event object rather than the individual message and value
we passed before. Therefore, if you use our AS 2.0 components with
the state engine, you'll need to add the following:
myEngine.handleEvent = function (ev) { this.ieh(ev.type, ev.val, ev.target); }
|
Included in the distribution
is our TestCases.fla
movie, which shows you how to code different configurations of state
machines.
Note:
Coding in AS 2.0, you can now declare the types of variables, such
as
var myState:state = new
state("state1", 0, myMgr);
However, you will get
a compile error if you try to define an internal event off the state,
is in
myState.onLeftButtonPress
= function () ...
because the state
class is not declared dynamic.
Here are some solutions:
- Remove the type restriction
from the variable. You will lose the checking, which is bad.
- (Purist approach)
Subclass the state
class with a new class of your own that declares the methods (such
as onLeftButtonPress)
you will use. This is the purist approach, but can be very time
consuming because you need to declare any method that any state
might declare.
- Go into your Flash
install directory then Classes
> mx > fmxis > FStEngOneFive and change the state
class definition to dynamic,
as in
dynamic class state
{ ...
Help Panel Documentation
While the engine is hard
to apply without our book, even when you have the book you have
to flip back and forth between the reference and your code. To alleviate
this, we have created full help panel documentation and code hinting.
You can see on the left of the figure below that we provide documentation
for all public events, properties, and methods for all core state
engine classes.
RTVis
The figure above is a
snapshot from the help panel screen for our run-time visualization
tool. This tool, nicknamed RTVis, dynamically reads the structure
of your state machine and displays it in a tree view component.
As your program executes, RTVis automatically updates the view regarding
the current state(s).
RTVis is a component
located in the RTVis folder within the Component Panel. It appears
as a pop-up window above your content on the Stage. You can inspect
the run-time state of your state engine by entering the full name
and path (from _root)
of the state engine. For example, if your engine is defined within
a movie clip instance named "myInterfaceClip",
and your engine was defined as "myEng
= new stateEngine(...);", then you would enter "myInterfaceClip.myEng"
into the provided text field, and then press "Load Engine".
RTVis will read the structure and display the current state(s),
if the engine is currently activated (if it is not, RTVis will still
show the structure).
When you are finished
using the tool, we recommend that you delete it from your Library
so that it does not bring in superfluous data before the first frame,
even when it does not exist on the Stage. See the help panel documentation
for more information.
Example
For those of you who
have attended our workshops and have built the simple traffic light,
we have extended the development to include a more sophisticated
pedestrian crossing system, as illustrated below. We can't give
you the code, but if you visit this
page you will see more information and we give you the XML statechart
description of the traffic light system.
The pedestrian crossing
system was built using the AS 2.0 version of FStEng 1.5,
and the cars were built with the AS 2.0 version of Miro Samek's
QHsm (from the November 2003 newsletter). If you
want to learn how to code these, attend one of our workshops!
Statechart XML to Code
Now Within Flash MX 2004 Environment
In the January 2004 newsletter,
we gave you a simple tool we built that generates FStEng v1.5 code
from an XML description of the state engine. We have taken this
tool on step further and packaged it into the Flash MX 2004 environment
directly. Once you install the provided MXP, the tool is available
in the Windows > Other
Panels > Statechart XML to Code menu:
When you call up this
window, you will see the following:
Follow the three steps
to produce code from an XML description. You can paste or type the
XML directly into the text box below step 1, or press "Select
XML File" to have Flash bring up a dialog box that lets you
choose the file from your hard drive or network (by the way, if
you are interested in the source code to see how this is done, please
click
here).
Once you have generated
the code, the program will tell you whether or not there were errors
produced in translating the XML to code. If there were errors, the
program puts comments with "ERROR" at the line it found
the error. In any event, you can copy all the generated code (Ctrl+A)
and paste it into the keyframe you want.
We have included two
examples in the Help book for this tool, so you can copy and paste
them into the box under Step 1 and see the code generated. The first
example is the infrastructure for a simple traffic light, and the
second is the complete XML specification for the Stolex watch interface.
For more information,
please ask us a question on our discussion
boards! |