g
Printer Friendly Version

editor  
BellaOnline's Flash and Animation Editor
 

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..

package
{
import flash.display.Sprite;
import flash.display.Shape;
public class DrawCircle extends Sprite
{
public function DrawCircle()
{
var myCircle:Shape = new Shape();
myCircle.graphics.lineStyle(2, 0x000000);
myCircle.graphics.beginFill(0xff0000);
myCircle.graphics.drawCircle(100, 200, 50);
myCircle.graphics.endFill();
addChild(myCircle);
}
}
}

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.




Flash and Animation Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 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.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor