Flash Concepts FAQ

What is SWF format?

SWF is a bytecode format that bundles Flash-usable resources, including code, animation, sound, and binary or text data. A SWF file's resources are accessible as a unified namespace from within Flash. This means that if you have a resource named "MyResource" in the SWF, you should have a corresponding class named "MyResource" in your code that can be instanced to access the resource. haXe's compiler model is code-centric and unlike the Flex compiler "MXMLC," haXe does not have a direct method of embedding arbitrary assets. Instead, we use asset-oriented SWF compilers such as Swfmill or SamhaXe to build a library SWF, and compile the code with the "swf-lib" option to create a final SWF with all assets bound together.

SWFs can host other SWFs, a powerful but complex feature that enables multi-loading, pre-loaders, and other arrangements. Each SWF contains its own namespace and is responsible for its own security. Inter-SWF access is allowed through the ApplicationDomain and Security classes.

SWFs, being originally designed as an animation-centric format, embed the notion of frames of animation into their core. Each frame of the SWF can contain any number of assets. This allows Flash-IDE generated animation to call up assets needed exactly when they have to be displayed onscreen - a useful feature for streaming long animations. It also allows a method of preloading in two frames; on the first frame the preloader code is run. The second frame contains the rest of the application. When the preloader sees that the full SWF has finished loading, it can either tell Flash to proceed to the second frame, or call code in the second frame directly.

What is the Stage? What are DisplayObjects?

Flash's visuals and interface are focused around the Stage class. This class is not instanced directly, it is accessed in haXe via flash.Lib.current.stage. There is only one stage; but recall that each SWF has its own namespace. From that we can see that "flash.Lib.current" refers to the current namespace, which automatically sits on top of the stage.

DisplayObjects are a set of classes that the Flash API understands how to use for drawing and interaction purposes. They can be added in a hierarchy, and input events can be attached to them. A clickable "Button" class, for example, might be composed as a Sprite with a TextField and a Shape as its children; the Sprite is the container object, while the Shape and the TextField draw the button graphics and the label, respectively. The Button would be made interactive with a call to addEventListener(). Finally, to make it show up on screen, one would add the Button(and not its children) to the stage.

There is a performance penalty for having a lot of DisplayObjects on the stage, because Flash has to traverse the hierarchy at least once to test and propagate events each frame.

I just want to draw bitmaps. How can I do that?

Create a new Bitmap object, and create a BitmapData for the Bitmap. The BitmapData contains the actual content of the bitmap; Bitmap, on the other hand, provides DisplayObject functionality, including rotation, scaling, filters, etc. Add the Bitmap to the stage, and you have a surface suitable for drawing. If you want to blit bitmaps, you have two choices: the slow draw() function which can access all the DisplayObject transformation functionality and render it into a new bitmap, and the fast copyPixels() function which does a straight blit routine.

How does timing in Flash work?

Flash uses an "elastic racetrack" model; this means that in each frame, some amount of time will be allocated between rendering and code, and the amount of time will adjust depending on previous frame performance.

SWF files specify a desired framerate, which has a major influence in the render/code tradeoff.

You have three general choices of timing: A Timer class, flash.Lib.getTimer(), and listening for the onEnterFrame event.

The onEnterFrame event will give the most reliable results if you want to lock your timing to the SWF framerate.

getTimer() is most useful for measuring time intervals, and can be used in combination with onEnterFrame to create delta-times if you want a FPS counter or a simulation that runs with a variable timestep.

The Timer class is good if you want "independently timed" actions going on simultaneously, or if you have a long-term process that needs to be split up to avoid the Flash Player's 15-second default timeout; you can use "new Timer(1,1);" to create a timer that fires in one frame.

What's the difference between Flash 9+ and older Flash?

Flash has a JIT VM starting with Flash 9; this change also included a new API. With Flash 10 additional API extensions have been introduced. To use the newest version use "-swf9 myswf.swf" and "-swf-version 10" (or whatever version is newest).

What are some performance tips for Flash?

In Flash 10 you have many options if you are bound by code performance.

First, make sure you are avoiding Dynamic and untyped{} where performance is critical; the JIT uses type information to speed things up. Second, you have the choice between Vector, List, and FastList as alternatives to Array(which is usually the slowest option). If you install ListTools, you may also use the list class included there, which is again optimized in another way from the other List classes.

Finally, you may use the flash Memory API, which lets you allocate a chunk of memory as a ByteArray and access its bytes directly.

If rendering is slow(it is considerably slower outside of the Windows player), avoid alpha transparency and usage of advanced DisplayObject features(rotation, scaling, 3D, filters). Prefer bitmaps to vectors; if you have a complex DisplayObject, you may want to try using cacheAsBitmap for a simple speedup. Try to reduce the quantity of rendering with early elimination tests, if they're possible and not too expensive.

version #8050, modified 2010-02-06 11:39:19 by rtf