Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/ianguth/ianguthridge.com/wp-settings.php on line 18

Strict Standards: Declaration of Walker_Comment::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/ianguth/ianguthridge.com/wp-includes/comment-template.php on line 0

Strict Standards: Declaration of Walker_Comment::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/ianguth/ianguthridge.com/wp-includes/comment-template.php on line 0

Strict Standards: Declaration of Walker_Comment::start_el() should be compatible with Walker::start_el(&$output) in /home/ianguth/ianguthridge.com/wp-includes/comment-template.php on line 0

Strict Standards: Declaration of Walker_Comment::end_el() should be compatible with Walker::end_el(&$output) in /home/ianguth/ianguthridge.com/wp-includes/comment-template.php on line 0

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method GoogleSitemapGeneratorLoader::Enable() should not be called statically in /home/ianguth/ianguthridge.com/wp-includes/plugin.php on line 339
The Basics of Coding in Flash | Ian Guthridge.com
Ian Guthridge.com

The Basics of Coding in Flash

This primer by no means covers everything about programming, but it covers enough to make you familiar with what's needed to create a your simple game.

  • Syntax: Syntax is a set of rules that defines what constitutes a valid program. The most important thing to remember about the syntax for AS3 is that except for comments and lines with opening or closing braces, all lines need to end with a semicolon.
  • Comments: A comment is a line, or set of lines that the computer ignores that lets the programmer put in notes to themselves so they can give themselves extra information about what the code is doing. This helps if changes are needed later, or multiple people are working on one project.
    Actionscript:
    1. //a comment
    2. /* this
    3. is
    4. a
    5. multi-line comment*/

  • Variables: Variables store values or states for other code to manipulate. They are more or less the programs working memory. In AS3, a variable is declared as follows:
    Actionscript:
    1. var x:int;

    Variables can be given an initial value when they are created, this is called declaring a variable:

    Actionscript:
    1. var y:int = 3;
    2. var foo:String = "bar";
    3. var z:int = y-45;
    4. var w:int = -z;

    There are a few basic types of variables that you will need to be concerned about right now:

    1. Integers: These are positive and negative whole numbers, like 1, 3, 15, -234, -9, 0, etc.
      Actionscript:
      1. var y:int = 3;

    2. Numbers: These can store decimals, like .46345634, -23.3342, 14, etc.
      Actionscript:
      1. var z:Number = .93753;

    3. Strings: These hold text values, like "Hello World!", "Jello", "a", etc.
      Actionscript:
      1. var f = "terrible";

    4. Booleans: These are either true or false.
      Actionscript:
      1. var flag:Boolean = true;

    Variable names are case sensitive, so aVariable, aVaRiAbLe, AVARIABLE, etc. are all different names. It is a good habit to get into to make variable names long enough to give some indication of what they are for; Avoid using single letter variable names unless you absolutely HAVE to. Numbers and underscores ( _ ) can be used in variable names, but a variable name cannot start with a number. So a_variable1, _aVariable1, and _1a are all legal variable names, but 1_aVariable would not be.

  • Arrays: Arrays let you store a bunch of values together, and access them easily via an integer index. The one tricky thing about arrays is that they start indexing from 0 not 1.
    Actionscript:
    1. var arr:Array = ["foo", "bar", "bat", "biz"];
    2. var  str:Srting = arr[2]; //assigns "bat" to str

  • Loops: Loops are one of the two basic kinds of "control structures", pieces of code that change the order that the lines of a program execute. Normally, code executes from the top of a file to the bottom, running each line once. A loop lets you repeat a line, or set of lines multiple times. The type of loop that is the most important for what you are going to be doing is called the for loop. A for loop will execute a block of code a specific number of times. A for loop looks like this:
    Actionscript:
    1. for(var i:int =0; i <30; i++){
    2.      //put some code here
    3. }

    "for(var i:int =0; i < 30; i++)" does three important things. It creates our iteration variable (i) it defines the conditional, that if false, causes the loop to terminate (in this case if i exceeds 30), and it defines how i will be incremented (i++ means i = i + 1). The part between the two braces { and } is the body of the for loop, in other words the code that will be executed every loop.

    For loops combined with arrays are really powerful:

    Actionscript:
    1. var names:Array = ["zero", "one", "two", "three", "four"];
    2. for(var i:int =0; i <names.length; i++){
    3.      //trace writes the output to a debug window,
    4.      // it doesn't do anything in a published .swf.
    5.      trace(names[i]);
    6. }

    This will print out each name on its own line.

  • Branches: Branching statements lets you execute one chunk of code instead of another piece of code. The branching statement I am going to concentrate on is the if statement:
    Actionscript:
    1. var bool:Boolean = false;
    2. var threshold = 15;
    3. if(bool){
    4.      //some code that won't execute
    5. }
    6. else if(17>= threshold){
    7.     //some code that will
    8. }
    9. else {
    10.    //more code that won't execute.
    11. }

    Any number of else if clauses can be put after each other, but only at most one else can be put with each if. Elses and else ifs are by no means necessary, but they give you more flexibility with your branching statements.

If you are confused in any way, or otherwise feel like you need a more complete primer on AS3, this site has a much more complete introduction to AS3.
Back to "How to Make a Simple Flash Game"

  • Share/Bookmark

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Cool places to visit!

Archives

All entries, chronologically...