-
prgrmr 4 hours user 4 years
classes ( may be a long one)
Ok this is my questions for classes.
In some classes I have run across this code
1. package
2. {
3. import flash.display.MovieClip;
4. public class Main extends MovieClip
5. {
6. public function Main()
7. {
8.
9. }
10. }
11. }
a. this code references mc's can it work with graphics and buttons as well.
b. what do the package, import flash, public class, and extends, functions do?
c. If I put these on a mc will that movie clip behave the way the code tells it to?
Proud member of The Absolute Noobs
-
It's kinda ... difficult, to explain OOP actionscript in a single post, so I'll just try to explain what's in this code.
1. Package declaration. Package is kinda like a folder directory list. It denotes what 'location' this file will belong in. If you have a package called com.flashkit.src, this .as code will probably be in the com folder, in the flashkit folder, in the src folder, in your hard drive, relative to your .fla program (eg. C:\Program Files\flashgames\com\flashkit\src\Main.as).
3. Import is a way of shortening class names. You wouldn't want to be typing flash.display.MovieClip everytime you needed to use it.
Actionscript Code:
import flash.display.MovieClip; import flash.display.Sprite; import flash.display.Bitmap;
//Or you can shorten it using this: import flash.display.*;
4. Public is a member access modifier. It can be used to modify members of a class eg. variables, functions, even classes themselves. Modifier include private, internal and protected. Public just means this class Main shall be accessible to all code. Understand that sometimes we need to restrict access of certain code in case of misuse or carelessness, hence the modifiers.
Extends means that Main shall inherit the "abilities" of MovieClip class. Essentially Main will be a MovieClip, and you might add additional functions/variables to it, and later on use [aka instantiate] it like var a = new Main(); However under this context, it might not be advisable to instantiate Main [see last section].
Note that external actionscript code [in .as file extension], must only contain a single class, and must have the same filename as the class. So this is probably Main.as.
6. Here you have a public function called Main. Because this function has the same name as the class Main, it is considered a Constructor. A Constructor is called whenever any such class is instantiated. Typically it's used to assign variables and data before usage, so that you don't have to do it manually after instantiation.
Note:
This code appears to be a Document class. A Document class is what you ask a .fla program to become. So when you create your .swf file, when you launch it it will be instantiated as a Document. If you didn't specify any Document Class your .swf defaults to a MovieClip, but if you used this code, your .fla will be a Main which is a MovieClip [due to inheritance]. You can see the entry for Document class here (mine is a Game.as):

Generally people don't try to instantiate their Document Class... well I haven't really tried it, might work but as a matter of principle is just wrong. 
Phew that was a lot of text. You need to get a book and read up if you really want to learn Actionscript 3. The plus is that once you've done that you can probably progress easily to C++, Java or C#. Or Javascript, for kicks.
-
Flash/Flex Developer
Cardin - great explanation. OOP in a single post is virtually impossible, but you are close.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
prgrmr 4 hours user 4 years
Thanks but now I have more questions. First off the package fuctions sets the directory the file will be in? But I thought the file was placed manually(or am I missing something). Also the package function doesn't have something like com.flashkit.src in my codes does it?
Also I'd like to thank you for your help, I suppose it would be better for me to learn it through practice.
Proud member of The Absolute Noobs
-
Flash/Flex Developer
As Cardin mentioned, when you create a class file you place it in a directory of your choosing. After you've decided on its location, then you update the class file by adding the package path. That path will mostly be different for each developer, based on what they prefer. A general rule of thumb for developers is to place class files within a com folder. Some com folders appear within a company folder, some are not.
The code you provided at the top of the thread does not contain any package path, which defaults it to same directory as the FLA file. If you move it from there, without that package path, it will generate errors, specific to the path.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
prgrmr 4 hours user 4 years
Proud member of The Absolute Noobs
-
Read Colin Moock - Essential ActionScript 3.0. It's an awesome book which explains almost every aspect of programming in actionscript 3
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|