So you have your movie ready to go, but
something just isn't working. Debugging your actionscript can be a tense
process but here are some tips to help you along.
Watching values
When working with complex chunks of actionscript
it often becomes necessary to track values in order to diagnose problems
in the code. There are three ways to do this:
- Trace Action. The Trace action allows
you to send a value to an output window when you test the movie in Flash.
Trace is excellent if you want to see a value at a number of specific
points in your movie. Remember to set the string vs. expression option
in the trace window. To get the value of a variable named frogs use
an expression like "Number of Frogs = "&frogs.
- List Variables. This option can be
found under the control menu when you test a movie. It lists every single
variable contained in your file at the time you select it. This can
be useful for mistakes in targetting variables such as typos.
- Text Fields. Probably the most useful
way of tracking variables in Flash is using text fields. These need
to be placed in the appropriate timeline but they have the advantage
of continuously reflecting values as the movie runs. They can be placed
in with the artwork or else off to the side.
Streaming and loading
Some problems arise because
Flash is a streaming format. Obviously Flash can't execute any code which
hasn't been downloaded yet. If you place chunks of code near the end of
the movie and refer to it earlier on then you must use a preloader to
ensure that it has been downloaded properly. This problem also comes up
when you use load movie or load variables. You need to allow for the time
it takes to perform the load action before you try to access any new variables
or code. After you issue the Load Movie action, trap the user in a loop
and test to see that the movie is fully loaded before you try to perform
any actions on it. You can check this by testing if GetProperty("_levelX",
_framesloaded) = GetProperty("_levelX", _totalframes).
Some common mistakes
- String vs. Expression. This one happens
all the time. Forgetting to set a value as an expression in a Set Variable
action can cause all sorts of havoc.
- Using numeric operators instead of string
operators. Remember that Flash has a different set of operators for
strings. Expressions such as "john"+"bob" will return a value of zero.
This is because when Flash sees a numeric operator it attempts to convert
both terms to a number. Make use of eq, &, gt, lt and the other string
operators found in the expressions editor.
- Missing or incorrect instance names.
Every instance of a movie clip must be given a unique name in order
to target it. Forgetting to put these in or making an error in spelling
will cause trouble.
- Knowing when to use eval. Sometimes
you want Flash to assemble a variable name through some sort of expression
and then return its value. For example the expression "name"&"1" will
return "name1". But if you want to retrieve the value stored in a variable
called name1 you must use the eval statement. The statement eval ("name"&"1")
might return "john".
- Variable scoping
and Tell Target. Remember that all variables are associated with a specific
timeline. When using Tell Target it is easy to forget the variables
declared outside of the Tell Target action are not local inside the
Tell Target action.
Last word...
Don't be afraid to make a mess. Maintain
multiple versions of your file and remember to back up your work so you
can safely experiment as you hunt down and eliminate problems. Another
good technique is to selectively cut down your work leaving only the part
which is causing you the trouble.
Good luck!
|