Week 8: In Class Practical – Intro to Actionscript 3.0

For this practical, we are going to work with the basics of programming in actionscript:

  • Variables
  • Data Types
  • Trace()
  • Math Operators
  • Functions
  • Function Parameters
  • Function Return Values

For each of the following exercises, create a new file and place it in a folder with your name in the week 8 practical folder on dropbox. By the end of the Practical you should have 4 separate files in your folder, one for each exercise.

1. Working with variables and simple math:

  • Create a new flash file and call it simple_math.fla. Create a layer called “actions” and be sure that it is locked so you do not mistakenly add any art work to it.
  • Open the actions panel by going to Window>Actions
  • Be sure you are on the “actions” layer on the timeline and you are also on the first frame.
  • Create two variables. Call them num1 and num2
  • Set num1 to be equal to 2
  • Set num2 to be equal to 4
  • **Remember – variable names call be anything as long as the do not contain letters or special characters. Try to keep your variable names short and descriptive
  • Create a new variable called “total” and set it equal to the sum of “num1″ and “num2″
  • Trace the value of “total” and test your movie by going to Control>Test Movie>Test
  • In the Output window you should see the number 6

2. Creating an addition function

  • In this exercise, we will build on exercise 1
  • Open your simple_math.fla file and go to File> Save As to create a copy of it
  • Name the copy adding_function.fla
  • Open the Actions panel once again and make sure you have the”actions” layer selected on the timeline and also make sure you are on frame 1
  • Here you should see the code you wrote in exercise 1
  • At the bottom of the code on a new line create a function and call it “addTwoNumbers” and make sure you set the return type to void
  • Find the code from above where you are adding “num1″ and “num2″ and setting their sum to the variable “total” and place it inside the function block (inside the curly braces)
  • Now find the code where you used the trace function to output the value of “total” and place that inside the function block (inside the curly braces) as well, but be sure it is place after the code the adds “num1″ and “num2″
  • Now that the function is complete we have to call the function, otherwise the code inside will never run
  • To run the function, call it by typing it’s name, in this case the name is “addTwoNumbers” and follow it with an open and close parenthesis and a semicolon
  • Now test your movie like before and you should see 6 appear in the output window once again.

3. Creating a function which accepts parameters

  • Create a Copy of adding_function.fla file and name it function_parameters.fla
  • In this exercise we will modify our function so it will accept two numbers that will be added together.
  • When a function accepts values of any kind those values are referred to as that functions parameters
  • We will start off by giving our parameters names that we can use to identify them
  • We do this by typing the parameter names in the parenthesis in the function definition. For example ours should look like this:
    function addTwoNumbers(firstNum, secondNum):void {
  • “firstNum” and “secondNum” will hold our values and we now have to replace “num1″ with “firstNum”, and “num2″ with “secondNum” inside our function
  • Now where we are calling the function we need to modify it to send our values
    addTwoNumbers(num1, num2);
  • Now call it two other times with any numbers you like
    addTwoNumbers(5, 30);
    addTwoNumbers(44, 100);
  • Test the movie and see if you adding function is working correctly

4. Returning a value from a function

  • Copy the file from exercise 3 to use as a starting point for this exercise and call it function_return_value.fla
  • In this exercise we will address the “:void” portion of our function
  • :void is use when we are not returning a value
  • If we are returning a value though we will need to modify this to say what type of value we would like to return
  • In our case we are dealing with Numbers so our return type will be a number and will look like this
    function addTwoNumbers(5, 30):Number {
  • Now where we are tracing the value of total within the function, we are instead going to return the value of total to the line that called the function.
  • To do this type
    return total;
  • Now our function is returning a value but nothing is currently receiving that value
  • To receive the value go to the line where we are calling the function and modify it to have a new variable called “mySum” be equal to the return value of the function
  • Then on a new line trace the value of “mySum” and test the movie


Comments are closed.