Radicate
Radicate

Reputation: 3054

Actionscript 3 OOP programming way?

I used to program simple stuff on flash using Actionscript 2 some time ago.. Now I'm trying to move on to Actionscript 3 and it seems like I'm managing to get used to the changes..

What I was wondering about was the way a lot of programmers I've seen are programming in..

For example this piece of code made me wonder about a few things:

package  {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class KeyExample extends Sprite {
        private var _theyArePressed:Object = { };
        public function Example() {

        }
    }
}

Why use the "package"? I always just put some AS on the first frame of the movie and that's it, is it wrong to do so nowadays?

Then I see that they import some stuff, but as far as I've seen I could use those things easily without importing a thing anyway, so what's the point here?

And about sprites? I'd love to know what they are for..

Thanks in advance

Upvotes: 0

Views: 886

Answers (6)

Marty
Marty

Reputation: 39476

package

This line defines the package or collection that the class you're about to define belongs to. It reflects the position of the .as file holding the class relative to the FLA (or additional source paths). The idea behind packages is to give your classes more context and to avoid class name conflicts. For example, you can make your own Sprite class and put it in your own package.

There is also an internal namespace that can be used to make a class only available to other classes that are in the same package.

import package.Class;

This line introduces external classes into the current class definition. It may seem annoying vs having everything available by default, but it works with packages and has many advantages:

  • It offers readability - you can determine which classes are being utilised in each other class by skimming over the top list of import statements.
  • You're able to define which class you want in cases where you have two classes named the same in two different packages (e.g. if you create your own Sprite class).
  • You can easily change the import statement to refer to an entirely different class of the same name in a different package.

You do not need to use import when you're working on a class that sits in the same package as another, you only need to import classes that are in a different package.

And about sprites? I'd love to know what they are for..

Sprite is an inbuilt class that inherits from DisplayObject, meaning it is used to represent graphics that can be drawn to the screen through the display list. It is one tier below the traditional MovieClip on the hierarchy of inbuilt DisplayObjects, as seen here:

enter image description here

What this means is that Sprites are lighter than MovieClips in terms of the process involved in having them rendered to the screen and the memory they use up holding information. The main difference between the two is that MovieClips have frames, whereas Sprites do not, meaning you will not have methods like gotoAndStop() or properties like currentFrame on a Sprite.

The idea for maximum results is to move as far up this tree as possible, to use the least resources through the lighter, less complex classes.

Upvotes: 1

Neil
Neil

Reputation: 8121

The reason we use packages is to avoid naming conflicts. I can create a class called button and use it in my components package, another developer can create a class called button and put that in his package. This way we can use both buttons in the same project without any conflicts.

We use the import statement to bring those classes in at compile time, without using import the class is not available. The reason for this is that you may have a big library with lots of classes in your project but you only want to use a certain amount of classes, if you didn't use import then all of them would be included and your resulting swf would be bigger than if you only import the classes you need.

Have a read of this article to get a firm understanding.

You use the Sprite class if you need to get something on to the display list using addChild etc. A Sprite is like a MovieClip without a timeline so it doesn't contain multiple frames. The first class you use in your app must be able to show something, so extending Sprite is usually the norm.

Have a read of this article to get an understanding of the display list and the classes you can attach to it.

Upvotes: 0

strah
strah

Reputation: 6742

It's not easy to answer your questions in a short SO style answer.

Best, just search for some OOP Flash tutorials. One can be found here: http://active.tutsplus.com/tutorials/actionscript/as3-101-oop-introduction-basix/

In short (the simplified answer):

  • package is a way the code in AS3 is written, it describes location of the file in relation to the project (again, simplified answer). For example you can put some .as file in the folder (don/graphics/shapes) so the package name would be don.graphics.shapes.
  • you need to import stuff if you write your code in external files. If you write code in the frames than Flash IDE can import some classes in the background, so some import statements can be omitted.
  • Sprite is a MovieClip without a timeline. Well, it has some more features (it's not dynamic, for example), but in most cases 'MovieClip with no timeline' is enough to know.

Upvotes: 1

lostPixels
lostPixels

Reputation: 1343

Packages are a great way to separate code. If you have a button that you want to use on multiple projects, you could make it a class. This follows the OOP approach because you're abstracting different parts of the logic.

Classes don't have access to everything, so you need to import other classes. If you'd like to use the Keyboard, then you simply have to import its class.

Upvotes: 0

M4tchB0X3r
M4tchB0X3r

Reputation: 1531

Using a DocumentClass and Classes (which are kept in packages)in general keeps the source cleaner, easier maintainable, interchangeable and makes it better to work in groups. So only advantages there ;)

You need all the imports because unlike on the Timeline script there are hardly any Classes Imported by default.

Sprites are like MovieClips without the Frames. So they are more lightweight. So if u don’t need frames, use Sprites!

btw, in ur snippet the funciton Name should start with a small letter ;)

Upvotes: 0

bitmapdata.com
bitmapdata.com

Reputation: 9600

If you want any .fla want to your Class use, do the following:

1: place .as file in specified directory. this directory is the top of the package.

2: click Flash-Preferences

enter image description here

3: click ActionScript3.0 settings... enter image description here

4: linked to your directory. enter image description here

Now can be used anywhere.

Upvotes: 0

Related Questions