-Scope-
Scope is what all of OOP abides by. It tells the program what variables, classes, and functions exist where and when.
say you have a function:
function myFunction():void{
var myVariable:Boolean = true;
}
myVariable will ONLY exist IN THAT FUNCTION, and NOWHERE ELSE.
same with this:
function myFunction(myVariable:Boolean):void{
myVariable = true;
}
it's the same with classes.
class MyClass{
var myVariable:Boolean = true;
}
there's one cool thing with classes, though. If you put a variable into a class, it can be used by ALL functions within that class.
class MyClass{
var myVariable:Boolean = true;
function MyClass():void{
trace(myVariable);
myVariable = false;
trace(myVariable);
}
}
There are some things you can do to either help scope along or break scope temporarily.
- Public
usage:
public var myVar:Boolean;
public members break the rule of scope that says "all variables and functions in a class can only be used in that class"
so, if you have this:
class MyClass{
public var myVar:Boolean = true;
}
you can do this:
class Main{
public var theClass:MyClass = MyClass();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
trace(theClass.myVar);
theClass.myVar = false;
trace(theClass.myVar);
}
}
- Private
usage:
private var _myVar:Boolean;
private members are the opposite of public ones, and stick to the rules of scope. The example above would not work if myVar was private.
private variables also have a leading underscore(_) to them. Not functions, however.
- Static
usage:
public static var myVar:Boolean;
private static var _myVar:Boolean;
static members break the rule that says "you have to have an instance of that class in order to use that class's members"
so, if you have this:
class MyClass{
public static var myVar:Boolean = true;
}
you can do this:
class Main{
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
trace(MyClass.myVar);
MyClass.myVar = false;
trace(MyClass.myVar);
}
}
instead of this:
class Main{
public var theClass:MyClass = MyClass();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
trace(theClass.myVar);
theClass.myVar = false;
trace(theClass.myVar);
}
}
works for functions as well, but be careful: A static function NEEDS to use static variables in order to work.
- Const
public const MY_VAR:Boolean = true;
private const _MY_VAR:Boolean = true;
public static const MY_VAR:Boolean = true;
private static const _MY_VAR:Boolean = true;
constants (const) are variables that are set right then and there and CANNOT BE CHANGED. Trying to change them results in an error.
constants are all uppercase, so CamelCasing can't really apply to them. Instead, use underscores to separate words.
-Events-
Actionscript 3 is event-driven, which means everything works in events and event handlers.
What do I mean by this?
public class Main extends Sprite {
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.ENTER_FRAME, gameLoop);
}
private function gameLoop(e:Event):void {
removeEventListener(Event.ENTER_FRAME, gameLoop);
}
}
init will add an eventListener that tells the program to run the function "gameLoop" as soon as every frame starts.
gameLoop removes the eventListener that init added as soon as it's run. So it only executes once. You can change that to whetever you like, though.
Any function that class has access to, you can add an eventListener of any kind to.
just remember: every eventListener function needs to have ONE parameter (and only ONE!) - that parameter is the event itself. After that, freedom is yours.
Look up eventListeners, there's a lot of cool things you can listen for that's built into Flash natively.
-Graphics-
Graphics. Finally, we're almost done! Graphics are a bit of a pain, but after that you shouldn't have a problem with anything.
1. Open Flash (yes, you finally get to draw!) - CS3 or greater only, please.
2. Create a new AS3 project.
3. Draw a square or something on the stage.
4. Select whatever you just drew.
5. Press the F8 key.
6. If the "Advanced" arrow pane thing isn't open, open it.
7. Give your symbol a name and a type, like normal. Name it like you would a normal public variable.
8. Add SWC_ to the beginning of whatever you named it.
9. Check the "Export for ActionScript" box
10. Check the "Export in frame 1" box if it isn't checked. Export in frame 1 if it isn't your default.
11. If it's a MovieClip and only has one frame, change the "Base Class" to flash.display.Sprite
12. Delete the object from the stage. It should still be in the library.
13. Hit "OK"
14. Go to File->Publish Settings...
15. Uncheck everything, check SWC
16. Hit "OK"
17. Save the .fla in your project's "lib" folder - I usually name mine "graphics", but it doesn't really matter
18. File->Publish
go back to FlashDevelop
1. Go to your "Project" tab and click on the little plus symbol next to the lib folder.
1.5. You should see your flash and .swc in there. If you don't, you saved your flash somewhere else and now need to go find it and put it in the right place.
2. Right-click the .swc and select "Add To Library"
now you can use your object as a class. How do you do this? Simple! Datatype a variable to whatever name you gave your object.
so in step 7 I named mine "myBox" and prepended "SWC_" to it like you should have done in step 8. So now my box is named "SWC_myBox"
now I just do this:
private var _myBox:SWC_myBox = new SWC_myBox();
don't forget the parenthesis!
so how do you add stuff to the stage? Also simple:
addChild();
so mine would be:
public class Main extends Sprite {
private var _myBox:SWC_myBox = new SWC_myBox();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
addChild(_myBox);
}
}
I don't know about you, but my box is in the upper-left. Let's change the defaults:
public class Main extends Sprite {
private var _myBox:SWC_myBox = new SWC_myBox();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
_myBox.x = 100;
_myBox.y = 200;
addChild(_myBox);
}
}
I change the x and y BEFORE I add it to the stage because it's much faster for the flash player to know where to put the box initially than it is to tell it to put the box at 0, 0 and then change it to 100, 200 instantly.
I mean, come on. That's just mean. Be nice to your computer.
Now you can access and change entire display objects through a simple variable. The variable acts as a normal variable with normal constraints.
-Sprite
A sprite is a movieClip with only one frame. For obvious reasons, this is MUCH faster than a MovieClip.
-swc
the .swc extention is basically a compiled list of classes.
Instead of having a whole bunch of .as files in your src folder, just compile them all into one .swc and use that.