10 brilliant Flash
tips Section: workshop > flash Author:
David Emberton Posted: 25/02/2003

There are some things that will never appear in the
Flash MX manual -- phrases like "to take advantage of this unsupported
feature" and "to work around this bug in the software". Confined to the
underground, these timesaving techniques become the tricks of the trade,
swapped under the dim glow of desk lamps across the country. Not your desk
lamp, perhaps, but that's what we're here for.
MOVIE CONSTRUCTION
Batten down those bitmap images
One of the longstanding bugs in Flash Player is commonly known as
"bitmap shift". As bugs go it is somewhat unpredictable and difficult to
reproduce, but it tends to show up whenever bitmapped images are being
animated. The effect itself is akin to warping, and is best described as
"hey, it moved without changing position."
There are several solutions to the bitmap-shifting problem, the most
popular being to keep the alpha property of the affected graphics to 99%,
so that they are never unanimated. A more recent trick is to create a
large, empty transparent PNG image with a single pixel of colour in the
top left-hand corner. Make it large enough to cover the stage, import the
image and then move it to -1, -1 so that the single pixel is not visible.
Place this image on the top layer of the main timeline, and the shifting
effect goes away as long as the PNG is present.
Choose an optimum frame rate
Flash Player 4 had a maximum achievable frame rate of 18 frames per
second. Versions 5 and 6 will attempt to play back at faster rates,
whenever possible, but taking things too far can result in excess
consumption of system resources, and slower overall performance. A frame
rate of 31 will provide good cross-platform performance on newer machines.
If you don't need extra smooth animation, throttle it back to 18.
Secure your files, for real
Security in Flash Player movies is inherently weak. You can write
password authorisation routines in ActionScript, but this script is easily
viewable in any hex or text editor. A new and highly effective freeware
product named Tevas (http://www.genable.com/aso/tevas.html)
seeks out references to passwords and keys you want to be secure, and
replaces that part of the SWF with an encrypted version.
Tevas also performs a certain amount of "obfuscation" on the rest of
your ActionScript code, making it difficult for decompiler programs such
as Burak's ActionScript Viewer to extract script from your movies. This is
particularly useful for sensitive or expensive-to-produce projects.
SCRIPTING
Use goto actions between scenes
This is an oldie but a goodie. Although goto actions can be used to
navigate between scenes on the main timeline, scene change actions do not
work from within movie clip symbols. This can be overcome by assigning
frame labels to the first frame of each scene you want to target.
For simplicity, use labels that match the name of each scene. As an
example, if you have scenes named Intro, Main and Catalog, assign the
frame labels "Intro", "Main" and "Catalog." That way, no matter whether
you're scripting the main timeline or a movie clip, you can use goto
actions such as this:
_root.gotoAndStop("Intro");
Note that _root refers
to the main timeline, regardless of where you place the code. When using
multiple levels, replace _root
with _levelx,
where x is the appropriate level number.
Access plain text from files and server applications
The latest iteration of ActionScript takes the loadVariables() action and supersedes it with the
much more flexible loadVars
object. One of the hidden advantages of loadVars is that it draws on many of the same
underlying methods as the XML object. loadVars is used to load text data written in
URL-encoded variable pairs, like so: var1=hello&var2=goodbye. But what if you just
want to load the contents of a simple text file or database field and
place them into a text field? Here's how:
myText = new
loadVars(); myText.onData = function(fileContents) { textField1.text
= fileContents; };
By using the undocumented onData method of loadVars and hijacking it with a custom function,
it's possible to intercept the text content before it goes for the usual
parsing. So you end up with the raw, plain text.
Refer to attached/created/duplicated movie clips
Flash MX offers several ways to create movie clip instances on the fly.
This is handy any time you have repeated elements, whether they are part
of an interface or an animation. When using attachMovie(), createEmptyMovieClip() or duplicateMovieClip(), you'll often need to
perform operations on the new instances, such as setting position
properties or variables. In Flash MX all three of these actions return a
reference to the new instance, which you can store in a variable. For
example:
var tempMC =
duplicateMovieClip("MC", "MC1", 1); tempMC._x = 100; tempMC._y =
100; delete tempMC;
The local variable tempMC automatically points to the duplicate movie
clip, regardless of where it is in the movie. The script shows it can be
deleted when no longer in use.
Attach imported symbols
Symbol linkage is a great feature. It allows you to share assets
between multiple movies, saving on bandwidth and editing time. But what
happens when you want to attachMovie() a symbol that's already being
imported at runtime? Usually it isn't possible, because a symbol with its
linkage set to Import cannot simultaneously be set to Export for
ActionScript.
There is a way around this. Simply drag an instance of the particular
symbol onto the stage, and use the Convert to Symbol command to embed it
into a new movie clip symbol. This is the clever bit: by setting the
wrapper to Export for ActionScript, the instance contained within is still
imported as before. You get the best of both worlds.
Import components
A similar but altogether different issue is the importing of shared
components. Flash MX components are tricky things; they're complex and
involve a lot of exporting for ActionScript and dynamic symbol attachment
as it is. With so many interdependent elements, it's not possible to
simply set, say, the ListBox component to Export for runtime sharing, and
have everything work as expected.
The wrapping technique comes in handy here also. To share a component,
drag an instance of it onto the stage, then wrap it in a new movie clip
and set that to Export for runtime sharing, specifying the import URL as
required.
Build dynamic paths with array access operators
In scripted applications, you'll often need to refer to a variable or
object in a generic way, since its exact name won't be known until the
movie is playing. This is common in games and other applications relying
on external data or user input. A sly method of constructing dynamic
references is to use array access operators. You can access the contents
of any movie clip using this method; the clip's child objects and
variables act like a named array. Here's an example:
for (i = 0; i < 5; i ++)
{ _root["sprite" + i]._alpha = 50; }
In the script above, the movie clips in the main timeline (sprite0 --
sprite4) have alpha properties set to 50%. In cases where you want to
address all objects in a timeline, you can also use a number (rather than
a named) index, such as _root[i].
PRODUCTIVITY
Quickly restore panels in Mac OS X
A major bug in the Mac OS X version of Flash MX is the application's
tendency to "lose" user interface elements like the Toolbox and Panels.
Rather than having to manually go to the Window menu and re-activate
necessary panels, create a keyboard shortcut for the Default panel set.
That way when things disappear, it's only a quick key press to solve the
problem.
To make a keyboard shortcut in Mac OS X, go to the Flash MX application
menu and choose Keyboard Shortcuts. Duplicate the current set, and then
scroll down to Window, Panels, Default set in the command list. Then use
the lower area of the dialog to assign a shortcut, such as Shift+Cmd 1.
|