Creating a Class with Flash ActionScript

Creating a Class with Flash ActionScript
In the past few tutorials, we have been discussing the options for including ActionScript in a FlashR project. The ActionScript code that we used in these tutorials drew a red circle on the stage. So far, we have just used this code for demonstration purposes. Now, it’s time to take a look at the code in more detail and learn how to create a class in ActionScript 3.

import flash.display.Sprite;
import flash.display.Shape;
public class DrawCircle extends Sprite
public function DrawCircle()
var myCircle:Shape = new Shape();, 0x000000);;, 200, 50);;

The first thing that you will notice is that the code that draws the circle is surrounded by what is called a package statement block. This is simply made up of the word “package”, the name of the package and opening/closing curly brackets. What it does is to work as a container or package that holds all the classes that we will use and prevents class naming conflicts. As you would expect, all of our classes for this Flash project will be “wrapped” inside this package statement.

package name_of_package { }

Did you notice that I do not have a name for my package in the sample code? According to the description, I need to supply a name for my package as one of the three things that make up the package statement. However, you will see quite often that programmers use the package statement without supplying a name for the package. When this is the case, Flash knows to use the default package.

Next we import Flash’s Sprite and Shape classes. By doing this, we gain access to the predefined Graphic properties and methods which we need in order to draw on the stage.

After the imports, we have the class statement.

public class name_of_class { }

Just like the package statement, the class statement has the word “class”, the name of the class and opening/closing curly brackets. But what is this word “public”? The word “public” is called a class access control namespace attribute. It controls the access to this class. A good analogy would be a website that has both a private and public area. Anyone can access and make use of the public area. But you need permission to access the private area. By using this public access attribute, we are telling Flash to allow all other code to access this class.

public class DrawCircle extends Sprite

The extends statement tells Flash that our new DrawCircle class “extends” the Sprite class. By doing this, Flash will let our new DrawCircle class use or “inherit” all the methods, properties and functions that come with the Sprite class.

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2019 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.