logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Full Schedule
g
g Flash and Animation Site

BellaOnline's Flash and Animation Editor

g

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.




Add Creating+a+Class+with+Flash+ActionScript to Twitter Add Creating+a+Class+with+Flash+ActionScript to Facebook Add Creating+a+Class+with+Flash+ActionScript to MySpace Add Creating+a+Class+with+Flash+ActionScript to Del.icio.us Digg Creating+a+Class+with+Flash+ActionScript Add Creating+a+Class+with+Flash+ActionScript to Yahoo My Web Add Creating+a+Class+with+Flash+ActionScript to Google Bookmarks Add Creating+a+Class+with+Flash+ActionScript to Stumbleupon Add Creating+a+Class+with+Flash+ActionScript to Reddit



 



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


For FREE email updates, subscribe to the Flash and Animation Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


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

g


g features
Motion 5 Parallax Effect - The Graphics

Animate Lower Third in Motion 5

Drop Zone and Lower Third in Motion 5

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor