Fandom

Kongregate Wiki

ActionScript 3 for Programmers

678pages on
this wiki
Add New Page
Talk0 Share

This article is intended to be a tutorial for people who already know how to program. If you don't have any clue how to program at all, we suggest going to another tutorial which isn't quite as fast as this one is. The Shootorials can be a good place to start if you don't have prior knowledge of programming.

VariablesEdit

Variables are initialized using the following syntax:

 var a = 2; //Without type declaration
 var b:Number = 2; //With type declaration

Declaring the type is completely optional, as Actionscript can use type inference. However, if you are programming something that requires speed (especially games), declaring the type is recommended. This way, when you assign a value to a variable, the AVM does not have to do type checks.

If you don't know the type of the object you're putting into the variable, you can also do this:

 var a:*; //initializes variable that can hold any type
 

BTW, when you initialize variables, it always holds a value that equals false if you don't set it. For example, if you set a variable to be a Boolean, by default, it will be False.

FunctionsEdit

Functions are defined using the following syntax:

 function myFunction(argument1:Number):void
 {
     //whatever the hell you wanna do here
     //If there was a real return type, you would have a "return foo" statement.
 }

Nothing special at all here. Note that the return type for functions is declared just as you would declare a type for a variable. If you're coming from a C/C++ background or Java background, please note that all arguments are passed by value rather than by reference. If you don't know what that means, it means that when you modify the argument, the original source of the argument doesn't change at all.

Actionscript also has this other feature called anonymous functions. This lets you create functions using the following syntax:

 var myFunction:Function = function():void
 {
 
 }

Because of this feature, functions are first-class variables, so they can be passed around as arguments to other functions. A popular example of usage of this feature would be a "map" function, which applies a function to every value in a list, array, or other similar container type.. (FYI, I don't mean those "key:value" container types, I mean a function called map.)


Passing default parameters into functions is also supported. For example, you have a function that traces output when a user clicks the mouse. However, you also want the option of calling the same function whenever you wish to trace out the location of the mouse.

 function traceMouse( event:MouseEvent = null ):void { }
  

This means that by default, the MouseEvent is null, so you can simply call the function anywhere in your program with or without using an Event Listener.

 package
 { 
   import flash.display.MovieClip;
   import flash.events.MouseEvent;
   import flash.ui.Mouse;
   
   public class Test extends MovieClip {
     public function Test()  
     {
       traceMouse();
       stage.addEventListener( MouseEvent.CLICK , traceMouse );
     }
 
     private function traceMouse( event:MouseEvent = null ):void 
     {
       trace( "Mouse X Position: " + mouseX );
       trace( "Mouse Y Position: " + mouseY );
     }
   }
 }
  

ClassesEdit

If you come from a Javascript background, this is the biggest difference from Javascript. In AS3, it's possible to create classes using the following syntax:

  class Hello
  {
 
  }
 
  

To create an external class file ( .as file ), wrap the class declaration inside the package tag. The file name, class name, and constructor name must all be the same. The next example would go into a file named Hello.as :

 package
 {
    // Any import statements go here, for example:
    import flash.display.Sprite;
 
    public class Hello
    {
       // Any Class Variables go here, for example:
       private var displayMessageText:String = "Hello!";
 
       // Constructor function
       public function Hello()
       {
 
          // Would trace to output panel: Hello!
          trace( displayMessageText );
       }
 
    }
 }
  

Access ControlEdit

ActionScript 3 has four possible access-control modifiers available for use.

  • public : can be accessed inside & outside of package, anywhere in your program
    • Constructors are always public
  • internal : can be accessed only inside the current package
    • internal is the default access-control modifier for all classes and class members
  • protected : can be accessed only inside the current class or by descendant classes
    • Classes and constructors cannot be defined as protected
  • private : can be accessed only inside the current class
    • Classes and constructors cannot be defined as private

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.