Variables%20 - PowerPoint PPT Presentation

About This Presentation
Title:

Variables%20

Description:

Flash ActionScript 3.0 Variables & Data types Thomas L vgren, Flash developer thomas.lovgren_at_humlab.umu.se Variables A variable is a place to store information It ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 22
Provided by: Thomas1378
Category:
Tags: media | types | variables

less

Transcript and Presenter's Notes

Title: Variables%20


1
Variables Data types
Introduction to
Flash ActionScript 3.0
Thomas Lövgren, Flash developer thomas.lovgren_at_hum
lab.umu.se
2
Variables
  • A variable is a place to store information
  • It has a name and a type
  • Variables are used to make the code dynamic
  • Values can generally be accessed or changed at
    any time
  • An identifier (usually a letter, word, or phrase)
    that is linked to a value stored in the system's
    memory or an expression that can be evaluated
  • Depending on the type system of a programming
    language, variables may only be able to store a
    specified data type

3
Naming variables
  • Variable names can only contain letters, numbers,
    and dollar signs ()
  • All variables must have unique names
  • Start variables with a lowercase letter
  • Variables are case-sensitive
  • Use mixed case for concatenated words
  • Don't use reserved words this, menu, private,
    video, etc.
  • Don't use the same variable name with different
    cases
  • Keep variables as short as possible while
    retaining clarity
  • Example, with strict datatyping
  • var xSpeedNumber

4
Data types
  • The Data type defines the type of data a variable
    or ActionScript element can hold
  • Primitive data types (Top level data types)
  • Boolean, int, Null, Number, String, uint, and
    void
  • Complex data types
  • Object, Array, Date, Error, Function, RegExp,
    XML, and XMLList

5
Variable syntax
  • Example of the different parts and structure of a
    variable declaration/population

6
Data type String (1/2)
  • Strings are sequences of characters, numbers and
    punctuation marks. These are enclosed within
    double (") quotation marks
  • The String data type represents a sequence of
    16-bit characters
  • The default value for a variable declared with
    the String data type is null
  • //declaration
  • var myURL_stringString
  • //assignment
  • myURL_string "www.flashkit.com"
  • //declaration and assignment
  • var myURL_stringString "www.flashkit.com"

7
Datatype String (2/2)
  • Example of some methods for the String object
    are
  • Substring, charAt, replace, toUpperCase, split,
    join etc
  • //declare variable
  • var my_stringString
  • //assignment and concatenation
  • my_string Hi there! //traces to Hi there!
  • //getting substring
  • var sub_stringString my_string.substring(3,
    my_string.length)
  • //making all characters uppercase
  • var upper_stringString sub_string.toUpperCase()
  • trace(upper_string) //yields THERE!
  • Tip! Declare your variables first (on top)

8
Data type Number (1/2)
  • The Number data type can represent integers,
    unsigned integers, and floating-point numbers
  • The Number data type uses the 64-bit
    double-precision format
  • The default value is NaN
  • //declaration
  • var lengthNumber
  • //assignment
  • length 1100
  • length -22
  • length 0.00002234
  • length 100/3 //traces to 33.3333333333333
  • length 1/0 //traces to Infinity

9
Data type Number (2/2)
  • Variable declaration, assignment and
    initialization
  • //declaration
  • var heightNumber
  • //assignment
  • height 200//literal value
  • height anotherVariable//value from another
    variable
  • //initialization(declaration and assignment on
    the same code line)
  • var widthNumber 300
  • //check max and min values
  • Number.MAX_VALUE 1.79769313486231e308
  • Number.MIN_VALUE 4.940656458412467e-324

10
Data type int
  • New data type in AS3
  • The int data type is a 32-bit integer between
    -2,147,483,648 and 2,147,483,647
  • Integers only work in whole numbers and ignore
    the decimal value, always rounding down
  • The default value for variables that are of data
    type uint is 0
  • //example 1
  • var myIntint -15
  • var myInt2int 3500
  • //example 2
  • var numint 2.5
  • var productint num 2
  • trace(product) //4, the decimal will be ignored

11
Data type unit
  • New data type in AS3
  • The uint (Unsigned Integer) data type is a 32-bit
    unsigned integer between 0 and 4,294,967,295
  • Unsigned integer or any non-negative whole number
  • The default value for variables that are of data
    type uint is 0
  • var myUnitunit 1
  • var mySecondUnitunit 3500
  • var unsignedIntegeruint //0

12
Number, int or unit?
  • To maximize performance, its recommended that we
    use the Number data type only for integer values
    larger than the int and uint types can store or
    for floating-point numbers.
  • To store a floating-point number, include a
    decimal point in the number
  • If we omit a decimal point, the number will be
    stored as an integer

13
Data type Boolean
  • Boolean represents a boolean value, possible
    values true or false
  • Converts the parameter expression to a Boolean
    value and returns true or false
  • Default vaule is false
  • //declaration of a boolean variable
  • var isLoadedBoolean
  • //assignmet
  • isLoaded true

14
Default Values (1/2)
  • A Default value is the value that a variable
    contains before you set its value
  • You initialize a variable when you set its value
    for the first time
  • If you declare a variable, but do not set its
    value, that variable is uninitialized
  • The value of an uninitialized variable depends on
    its data type

15
Default Values (2/2)
  • The following table describes the default values
    of variables, organized by data type
  • Note! For variables of type Number, the default
    value is NaN (not a number)

16
Array (1/2)
  • Arrays are lists of data under which each item is
    identified by its order within the list
  • The first element in an Array has index 0
  • An array can be made up of primitive type values
    like strings, numeric values, booleans or complex
    type values like other arrays or objects
  • var music_arrayArray new Array()
  • music_array "Metallica", "Bruce Springsteen",
    "U2", "Iron Maiden", "David Gray", "Van
    Morrison"
  • music_array.length //traces the length of the
    array
  • music_array1 //traces Bruce Springsteen
  • var my_arrayArray 5,"Hello!",a5, b7
    //complex array

17
Array (2/2)
  • Example of some methods for the Array object are
  • slice, join, concat, push, pop, reverse etc.
  • var music_arrayArray new Array()
  • music_array "Metallica", "Bruce Springsteen",
    "U2", "Iron Maiden", "David Gray", "Van
    Morrison"
  • //slice(startIndex, endIndex)
  • music_array.slice(2,4) //traces U2, Iron Maiden

18
Typecasting
  • Sometimes we need to typecast a data type into
    another (for some reson), for exampe if we load
    XML data (numbers) and Flash interpret it like
    string vaules
  • Heres an example of how we can typecast a string
    into a number
  • var my_stringString "50"
  • var my_numNumber 20
  • var answerNumber
  • trace(my_string my_num) //traces 5020
  • //typecast the string to a number data type
  • trace(Number(my_string) my_num) //traces 70

19
Scope
  • Scope is the realm or space in wich an object
    (variable) lives
  • In ActionScript 3.0, variables are always
    assigned the scope of the function or class in
    which they are declared
  • Entrance into that scope typically begins a
    variable's lifetime and exit from that scope
    typically ends its lifetime
  • function localScope()
  • var strLocalString "local"
  • localScope()
  • trace(strLocal) //error because strLocal is not
    defined globally

20
Arithmetic operators
  • Arithmetic operators
  • , -, , /,
  • , - -
  • , -, , /,
  • Increment , and decrement - -
  • Increments/decrements a variable by 1
  • var xNumber 10
  • x
  • trace(x) //traces 11
  • x--
  • trace(x) //traces 10

21
Precedence
  • The answer depends on operator precedence
  • var iNumber
  • i 12 3 10 / 2 //traces 27
  • You can override precedence by using parenteses
  • var iNumber
  • i(12 3) 10 / 2
  • trace(i) //traces 75
Write a Comment
User Comments (0)
About PowerShow.com