Developing Android Applications with Adobe AIR [116]
package {
public class myClass extends EventDispatcher {
public function myClass() {
loadOpening();
}
function loadOpening():void {
// load a swf with a timeline animation
background.addEventListener("OpeningDone", removeBackground);
}
function removeBackground(event:Event):void {
removeChild(background);
}
}
}
The code on the last frame of the opening .swf is:
stop();
dispatchEvent(new Event("OpeningDone"));
In the current event model, the listener function only receives one parameter: the event. If you want to pass additional parameters, you need to write your own custom class that inherits from the Event class. In the following example, the CustomEvent class has an event of type CONNECTED. It passes a parameter called parameters of type Object:
package {
import flash.events.Event;
final public class CustomEvent extends Event {
public static const CONNECTED:String = "connected";
public var parameters:Object;
public function CustomEvent(type:String, parameters:Object = null) {
// type, bubbles, cancelable
super(type, true, true);
this.parameters = parameters;
}
// this is needed if the event is re-dispatched
public override function clone():Event {
return new CustomEvent(this.type, this.parameters);
}
}
}
addEventListener(CustomEvent.CONNECTED, onConnected);
dispatchEvent(new CustomEvent(CustomEvent.CONNECTED, {name:"somebody"});
Diagnostics Tools
You can monitor performance and memory using diagnostics tools. Let’s look at a few of them.
Hi-Res-Stats
The Hi-Res-Stats class, from mrdoob, calculates the frame rate, the time to render each frame, the amount of memory used per frame, and the maximum frame rate and memory consumption. Import the library and add a new instance of Stats as a displayObject. This is simple and convenient to use (see https://github.com/bigfish):
import net.hires.debug.*;
var myStats:Stats = new Stats();
addChild(myStats);
Because it needs to be added to the displayList and draws its progress visually, as shown in Figure 19-4, this tool may impact rendering slightly, or get in the way of other graphics. A trick I use is to toggle its visibility when pressing the native search key on my device:
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
function onKey(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.SEARCH:
event.preventDefault();
myStats.visible = !myStats.visible;
break;
}
}
Figure 19-4. Hi-Res-Stats display
Flash Builder Profiler
The premium version of Flash Builder comes with Flash Builder Profiler, which watches live data and samples your application at small, regular intervals and over time. It is well documented. Figure 19-5 shows the Configure Profiler screen.
Figure 19-5. The Configure Profiler screen in Flash Builder Profiler
When “Enable memory profiling” is selected, the profiler collects memory data and memory usage. This is helpful for detecting memory leaks or the creation of large objects. It shows how many instances of an object are used.
When “Watch live memory data” is selected, the profiler displays memory usage data for live objects. When “Generate object allocation stack traces” is selected, every new creation of an object is recorded.
When “Enable performance profiling” is selected, the profiler collects stack trace data at time intervals. You can use this information to determine where your application spends its execution time. It shows how much time is spent on a function or a process.
You can also take memory snapshots and performance profiles on demand and compare them to previous ones. When doing so, the garbage collector is first run implicitly. Garbage collection can also be monitored.
Flash Preload Profiler
The Flash Preload Profiler is an open source multipurpose profiler created by Jean-Philippe Auclair. This tool features a simple interface and provides data regarding frame rate history and memory history, both current and maximum.
Other more unusual and helpful features