Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Working with External Code

Working with ActionScript in an external file and including or importing it in your application has many advantages:

  • It allows you to have some separation in your workflow between Flash and the stage on one monitor (if you are using a stand-alone ActionScript editor), and have all of your code displayed at the same time on another monitor.
  • It is a good choice when multiple developers are all coding different aspects of the same application.
  • It is the start of getting into and appreciating class-based programming using ActionScript.
  • Many developers are not that fond of the ActionScript panel in Flash and prefer third-party ActionScript editors.

To create an external .as file:

  1. Choose File > New > ActionScript File from the menu bar to open the New Document dialog shown in Figure 4.3. The new .as file displays and you will see only the ActionScript panel interface.

    Figure 4.3

    Figure 4.3 Creating a new ActionScript file.

  2. Add the following ActionScript to the .as file to add numbers in a function, return the sum, and display it in the Output panel:

    function addValues
      (val1:Number, val2:Number):Number {
       var sum:Number = val1 + val2;
       return sum;
    }
    var sum:Number = addValues(6, 8);
    trace(sum);
  3. Save the file to a directory of your choice and name it chapter4_include.as.

Next you need to add this .as file to the timeline of a Flash movie.

To include an ActionScript file:

  1. Create a new Flash file and save it as chapter4_main.fla in the same directory as the chapter4_include.as file.
  2. Rename Layer 1 to Actions and open the Actions panel.
  3. Add the following ActionScript to include chapter4_include.as in the timeline:
    include "chapter4_include.as";
  4. Preview your movie; you should see the result 14 display in the Output panel. This was the result of the ActionScript from the external .as file compiling in the Flash movie.
  • + Share This
  • 🔖 Save To Your Account