How to Use Variables in AS3

Variables are an essential and fundamental element for the creation of ActionScript 3 (AS3) programs with Adobe Flash multimedia software. The most fundamental AS3 variables are used to perform numerical calculations and text manipulations These variables, number and string variables, are used in Flash animation and business programs, Using them requires that you first declare the variable and type and then assign a value to the variable. Once the variable values are declared and assigned, you then use them to define mathematical calculations and perform string operations needed for animation and business programs.

Instructions

  1. Use of AS3 Number Variables

    • 1

      Declare a number variable with a variable declaration statement. Use a var statement to assign a name to the variable and specify the type of variable as number. Use the general AS3 code syntax to declare a number variable as follows:

      var <variable name>: Number

      For example to declare a number variable that has been given an arbitrary name of "A" use the code

      var A:Number

    • 2

      Assign a decimal number to the number variable. For example, to assign (or store) the number 3.14 in a variable called "A" use the code

      A = 3.14

    • 3

      Perform multiplication calculations with the multiplier ope rater "*." For example, to multiply the numbers stored in two number variables, A and B, and store them in a third number variable named C, use the code

      C = A*B

      If A was 2.1 and B was 2.0 the result stored in C would be 4.2.

    Using AS3 String Variables

    • 4

      Declare the string variable, a variable that stores text characters, with a variable declaration statement that specifies the variable type as a string. Use the general AS3 code syntax to declare a string variable:

      var <variable name>: String

      Where variable name is an arbitrary name for the string variable. As an example to declare a string variable, named B, as a string variable use the AS3 string variable declaration statement

      var B: String

    • 5

      Assign (store) text in the string variable, named B, with the code

      N= "this text will be displayed on the screen"

      Note that the text to store in a string variable must be enclosed in a quotes.

    • 6

      Concatenate (connect together) two string variables stored in string variables AString and BString in a third string variable called CString with the code

      CString = AString + BString

      If AString was assigned the text "The students first name was" and the variable BString was the assigned the text " Mark" the value of CString would be "The student's first name was Mark." Note that a space was deliberately used in " Mark" so that when the strings are concatenated together there is a space between "was" and "Mark" in CString.

Learnify Hub © www.0685.com All Rights Reserved