ActionScript 101 Advanced Functions (part 6.2)

Follow @tristarweb

In our last tutorial we looked at creating a basic two value calculator script in Flash ActionScript 3. This time we are going to have a look at that code and start to break it apart.

Let take a look at the code:

function myCalculator (firstNumber:Number, secondNumber:Number, calcType:String):Number
{
  if(calcType=="+")
  {
    return firstNumber+secondNumber;
  }
  else if (calcType=="-")
  {
    return firstNumber-secondNumber;
  }
  else if (calcType=="*")
  {
    return firstNumber*secondNumber;
  }
  else if(calcType=="/")
  {
  return firstNumber/secondNumber;
  }
  else
  {
    return 0
  }
}

This is a simple script based around conditionals statements , and will be your first look at if statements and conditionals operators if you have been following this post series.
One other main aspects to this script is the ability to send values to the function to be processed and a value to be returned, we will look at this aspect in more detail in a later post.

Lets first take a look at the conditionals “if”, “else if” and “else”. These are actually quite simple and should be nothing to fear, you are in essence telling your script to run certain sections of code “if” certain conditions return true. The conditions can be as simple as ” is value1 equal to value2 if so then run this script”

Lets take a look at an example:

var value1:Number = 1;
var value2:Number = 3;

if (value1 == value2)
{
    trace("the two values match");
}
else if(value1 > value2)
{
    trace("value1 is greater than value2");
}
else
{
    trace("none of the conditions are true");
}

The above script first checks – IF “value1″ is equal to “value2″ it of course returns false so does not run the script within the if statement. We therefore move onto the “else if” statement, the “else if” statement will run IF the script before it returns false, in this case we are checking if value1 is greater than value 2 and again it returns false so moving on to the else statement. The else statement will run if all the other statements return false.

These statements are very useful in running different code dependent on the conditions which exist in the program the users interaction or the programs environment. These statements for example could allow you to run different code if the user is viewing on different browsers, different times of day, different user entered values as with the calculator script, but fundamentally if there is a value to be quantified you can use conditional statement to check them.

The final piece to this puzzle which I have not gone into any detail of yet is the logic operators within the if statements: these are simple but their implementation can be relatively difficult if not used correctly so the next post will look at logic operators.

These if statements are fundamental to the calculator script above and we need to look at them closely to understand the calculator script. Take a look back at the calculator script above now keeping in mind what we have learned, it should now make a lot more sense.

About russell

Web Developer I’m a web developer with 3 years’ experience in the media industry.My Development skills includes flash, AS3, AS2, HTML, CSS, JavaScript, Jquery, XML, PHP, and MySQL. I have a great deal of experience developing interactive user interfaces in PHP Javascript AS3 AS2I have two masters degrees in Computer Science and Multi Media and before my current role I completed a year of research at De Montfort University researching multi touch technology. during this time I developed several multi touch applications for entertainment spaces.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter