FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Professional Adobe Flex 3: Creating an ActionScript Application

By: Joseph Balderson

Page 1 of 2

Set for printing

Next

In May 2009, Wrox released a ground-breaking book: Professional Adobe Flex 3, written by  Joseph Balderson, Peter Ent, Jun Heider, Todd Prekaski, Tom Sugden, Andrew Trice, David Hassoun and Joe Berkovitz, (ISBN 0470223642). This landmark publication consists over 1400+ pages of Flex reference material, covering a multitude of topics ranging from beginner to advanced, from the fundamentals of the Flash Platform technology ecosystem to the intricacies of the most advanced Flex-related frameworks and server products, in a breadth and depth found nowhere else.

In a Community MX exclusive, the following is an excerpt from Chapter 6: Using Flex Builder 3. In this tutorial you will build a simple ActionScript application, and learn all about the undocumented [SWF] metadata tag to configure the Flash Player publishing properties.

Adobe Professional Flex 3

Creating an ActionScript Application

In this exercise, you're going to learn a bit more about Flex Builder 3 by creating an ActionScript application in Source mode.

Most of the time you spend in Flex Builder will probably be creating code in Source mode, using Design mode only for quick mockups or prototypes, or to check the layout of certain UI.

Creating an ActionScript Project

Creating an ActionScript project means that using the Flex framework is optional, and must be explicitly imported for the application to be compiled with the Flex framework. This means you can, if you wish, create SWF applications with a small file size footprint that depend solely on the ActionScript 3.0 and Flash API classes.

  1. Right-click in the empty Navigator View, and select New > Flex Project.
  2. In the resulting screen, shown in Figure 6-17, fill in the name of the project as Chapter_06_AS_Project.


    Figure 6-17

  3. Click Next.
  4. This screen will set the build paths and the application filename for the ActionScript project (see Figure 6-18). Set the Main source folder as src (short for "source"), and the Main application file as DrawCircles.as. Leave the other fields as per their defaults.


    Figure 6-18

  5. Click Finish.

    As shown in Figure 6-19, you should now see the project in the Flex Navigator View, and the DrawCircles.as file loaded in the editor containing the following default code:

    package {
        import flash.display.Sprite;
    	 
        public class DrawCircles extends Sprite
        {
            public function DrawCircles()
            {
            }
        }
    }
    

    Notice that DrawCircles extends Sprite by default and not a Flex framework class. By default the editor defaults to the Source Mode Editor, since you can only use Design mode when working on an MXML file.

    Notice that the Outline View now displays the default class structure for the DrawCircles class you just created.

    Figure 6-17
    Figure 6-19

  6. Add the source code shown in Listing 6-1 to the file.
    package {
        import flash.display.Graphics;
        import flash.display.Shape;
        import flash.display.Sprite;
        import flash.display.StageScaleMode;
        import flash.display.StageAlign;
       
        public class DrawCircles extends Sprite
        {
            public function DrawCircles()
            {
                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = StageAlign.TOP_LEFT;
                doDrawCircle( 0x336699 );
                doDrawCircle( 0x993333 );
                doDrawCircle( 0x339933 );            
            }
    
            private function doDrawCircle( color:uint ):void
            {
                var child:Shape = new Shape();
                child.graphics.beginFill( color );
                child.graphics.lineStyle( 2, 0xCCCCCC );
                child.graphics.drawCircle( 30, 40, 30);
                child.graphics.endFill();
                child.x = (this.numChildren * 65) + 10;
                child.y = 0;
                addChild(child);
            }
        }
    }
    

    Listing 6-1

    The first two lines in the constructor function set the stage properties so that the SWF will appear at the top left and will be resized rather than scaled in the browser. The constructor then calls the doDrawCircle method, passing a hex color number for the new circle.

    The doDrawCircle method uses the Flash drawing API (you could say the "Flash 6 Drawing API," not the new "Flash 10 Drawing API") to draw a gray circle with a fill, the color that has been passed to its argument. It then positions the new circle according to the current number of circles already created and adds the object to the stage with the addChild method.

  7. Run the application. You should see a blue, a red, and a green circle on the stage.

    See Chapter 8 for more information on using the code editor in Flex Builder.

Now let's compare file sizes between the two applications from this chapter.

Comparing File Sizes

  1. Navigate to the bin-debug folder in the Flex project, and right-click > Properties on the Main.swf file. You will observe that the Main.mxml application is about 276KB.
  2. Navigate to the bin-debug folder in this ActionScript project, and right-click > Properties on the DrawCircles.swf file. You will find that the file size is an astounding 1KB.

This demonstrates just how much the Flex framework adds to a SWF file. If the file size of your SWF is a concern, such as the development of small embedded Flash widgets or online Flash advertising, it pays not to use the Flex framework. (Shhh! Don't tell anyone I told you that! This is a Flex book, after all!) For more information about the class APIs available without the Flex Framework, see Chapter 4, "Using ActionScript 3.0." For a review of where Flex fits into Flash platform development, see Chapter 1, "Why Flex?"

Page 1 of 2 1 2 Next


download
Download Support Files


Keywords
Professional Flex 3, Flex, Flex 3, book, Wrox, excerpt, actionscript, swf metadata, flex builder