Google+

Syntax Error?

Leave a comment

March 13, 2013 by jonmillymiles


The one thing that I hate about programming is bugs. Not the creepy little things that you eat as part of a Bush Tucker Trial, but the pesky little blighters that continually ruin your life when you are programming.

Fortunately for me I have had a little bit of experience programming (I reserve the right to say that I am in no way an expert!!) and understand the basic syntax of Actionscript, so I thought I would write a short article on the syntax of Actionscript 2, some of the other common mistakes I have made and some practical steps that you can take to reduce them.

Terminology

Within programming there are several terms used to refer to symbols and unless we are specific we could become easily confused. These symbols have correct names that we will need to use to avoid confusion:

[ ] – Square brackets. These are used for defining an index for an array.

myArray[0]; //First item of an array
myArray[1]; //Item number 1 of an array

( ) – Round brackets – Parenthesis. These are used for many things, for creating an order within a mathematical formula; for enclosing logical expressions within a conditional statement; for passing variables to functions – note that if no variables are passed then they are still included but with the close parenthesis immediately after the open – ().

functionName = function ( variable1:Number, variable2:String) {
     a = b;
}

{ } – Curly brackets – Braces. These are used to contain the commands that will be executed by a conditional statement or a function.

functionName = function () {
     a = b;
     c = d; 
}

; – Semi-colon. This character ends EVERY line of commands within Actionscript.

var a:number = 0;
a = 3;

. – Dot. This character is used to identify sub-objects, functions of objects and variables of objects.

_root.playerShip_MC._x 
_root.playerShip.onEnterFrame
this.alpha

// – In-line comments. The double backslash allows you to enter a comment on that line. Anything after an in-line comment on the same line is ignored.

a = b; // This text is ignored

/* … */ – Block comments. These allow you to block out chunks of code and large blocks of text. Anything between block comment markers is ignored.

a = b;

/* This is ignored.
And this.
And this. */

thisIsNot = true;

Syntax

Rule #1: Every command line must end with a semi-colon ;

This tells the compiler, the program that converts your code to instructions for the computer, where one command ends in a sequence and where the next begins.

Rule  #2: Every open bracket must have a close bracket.

Sounds simple doesn’t it, but leaving out a single close bracket can cause multiple errors when you test your script. Always check the first line in your list of errors as this normally contains the faulty statement. Once corrected, test again and see how many errors you get.

Rule #3: Upper case?

The compiler used within Flash is case sensitive and so variables, functions and commands must always be spelt correctly: onenterframe is not the same thing as onEnterFrame and it is not used in the same way.

Rule #4: Variables must begin with a letter.

While variables may contain the underscore character and numerical characters they cannot begin with them. So no more _playerLives or 5thEnemy.

Fault finding tips

Tip #1: Comment, comment, comment, comment, comment. Commenting helps you to understand what you want your code to do, it also helps others understand what you want your code to do. This helps you to check your plan against your logic and helps others to see what you are trying to do – your plan AND your logic.

Tip #2: Know what you want your program to do. It sounds simple but if you just throw code at the screen with no plan of what you want it to do you are asking for trouble. 5 minutes planning on paper or in pseudo-code can make your logic much easier to write and understand.

Tip #3: Consistency is key. When creating variables and objects find a way of describing them and naming them that is both natural to you, meaningful to others and easy to read. It is common practice to use “inter-caps” or “CamelCase” when naming objects, variables and functions. This is where the initial letter of the item is lower case and the rest of the words that make up the name of the variable begin with an upper case letter for example:

playerNumberOfLives
enemyShipBullet
distanceTravelledByPlayer

Often mistakes are caused through typing errors in the names of items due to using many different styles of naming: if you consistently use one style you will be less likely become confused.

Tip #4: Indent. Indenting code after an opening brace allows you to see the construct of your program much easier. It also helps identify when close brackets are missing. For example:

function testBraces() {
      if (bracesOpen == 5) {
            playerShip._x += 5;
      } else {
            playerShip._x -= 5;
      }
 }

Tip #5: Trace often. The trace command is one of the most useful that you can use when debugging. If you are writing a new section of code place plenty of trace commands in it so that you can see what is going on with the variables within the code. It is also a good idea to place a trace command to tell you when a section of code has been activated and this can lead  you to flaws in your logic or syntax. For example

function buttonPressed () {
       trace ("The button has been pressed");
 }

Every time the function is called the “The button has been pressed” message is displayed in the output window. This way if the message is never shown then there is a problem with your code calling the function. Similarly:

if (bullet._x > 800) {
           trace ("X value of bullet: " + bullet._x);
           trace ("The bullet is off the screen");
     }

Every time the bullet has an _x value greater than 800 the x value of the bullet and the  message will be displayed.

Tip #6: Check and double check the the names of variables, objects and functions. Make sure that what you have on your movie clip ID is the same as what you have in your script is the same for your variables is the same for your functions. This is why consistency, and making a habit of it, is so important. For me this has been the most common bug by far, especially when tired or coming back to a project after a short break.

Tip #7: Research, research and ask for help. We are not the first people to do this and you will not be the first person to have that particular problem, so look for the answers online. Remember to make sure that you are looking at code for the version of Actionscript that you are using, and that you include the name of the command that you wish to use. If you want to do something but don’t know the command to use search for the desired result. Just remember that copying and pasting code is not going to get you very far, you need to understand it and how it works. If all else fails ask someone else. Personally I will expect to see an honest attempt at writing and understanding the code for the desired effect before I sit down with you. This means that you have tried and if you have tried yourself then I will be helping… not doing the work for you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats

  • 9,998 hits

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 580 other followers

Archive

%d bloggers like this: