Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Creating ActionScript in External Files

4. Creating ActionScript in External Files

Lesson overview

In this lesson, you will learn to do the following:

  • Create an ActionScript class that extends the MovieClip class.

  • Create a constructor function.

  • Add methods and properties to a class file.

  • Define parameters for class methods.

  • Use ActionScript code to create vector shapes.

  • Use code to create instances of a custom class file in the Flash timeline.

  • Use the MOUSE_MOVE event.

  • Turn off an event listener.

  • Generate random colors.

This lesson will take approximately 2 hours.

Creating external ActionScript files can be a convenience for reusing code, or it can be the foundation for building large applications that use object-oriented programming practices (OOP).

Each mouse movement in the Lesson 4 project produces a trail of ellipses with a different random color.

Up until this point, all the lessons have created ActionScript in frames of the Flash timeline. This is a very useful way to work, and many Flash developers create all their projects exclusively using this method. For simpler projects, this has the benefit of keeping all the graphics and media in the same file as the ActionScript. However, for Flash projects that are more complex, it is often cumbersome to have hundreds or even thousands of lines of code in the timeline.

The alternative is to store the ActionScript for a project in one or more external files that just contain code. These ActionScript files can then be integrated with graphics, animation, and other Flash content to create the final project. External ActionScript files are plain text files saved with an .as suffix.

There are many benefits to developing larger projects in this manner. Most importantly, it means that the functionality of your applications can be divided into reusable chunks of code.

One characteristic that makes for a successful ActionScript file is that it’s often written to be as versatile as possible. For example, you could create an ActionScript file called that contains code to keep track of a user’s score. By writing the code such that some of its properties could be individually modified, the code could be used in a game where the user gets 10 points for shooting space aliens and needs 1000 points to win or in a history quiz where the user gets 1 point for each correct answer and proceeds to the next lesson when the score reaches 20 points.

In this lesson, you will get some experience creating an external ActionScript file designed to generate a simple graphic. You will then use this external .as file in a very simple Flash painting application, to determine the shape of the brushstrokes.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint