O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Real UI Component Example

To demonstrate how to use ActionScript to create and control Flex framework UI components, we'll create a simple application that contains only two component instances: a Button instance and a DataGrid instance. The application simply counts the number of times the Button instance has been clicked. The DataGrid instance displays the total click count, and the amount of elapsed time between clicks.

We'll name our application's main MXML file MinimalMXML.mxml. We'll name the class that defines our program's point of entry Clickometer.

Here's the code for MinimalMXML.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="vertical" applicationComplete="Clickometer.main()">
</mx:Application>

In the preceding code, notice that the value of the applicationComplete event property indicates the method to invoke (Clickometer.main( )) when the application has finished initializing. Notice also that the application uses a "vertical" layout scheme. For information on layout options, see the Application class's instance variable layout in Adobe's ActionScript Language Reference.

Now here's the code for the Clickometer class, where the UI components are created:

package { import mx.controls.*; import mx.core.*; import flash.events.*; import flash.utils.*; public class Clickometer { private static var lastClickTime:int = 0; private static var numClicks:int = 0; private static var grid:DataGrid; private static var button:Button; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required