Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables & datatypes Flash ActionScript Introduction to.

Similar presentations


Presentation on theme: "Variables & datatypes Flash ActionScript Introduction to."— Presentation transcript:

1 Variables & datatypes Flash ActionScript Introduction to

2 Variables A variable is a place to store information A variable is a place to store information It has a name and a type It has a name and a type Variables are used to make the code dynamic Variables are used to make the code dynamic Tip: Declare your variables first (on top)

3 Datatypes The datatype defines the type of data a variable or actionscript element can hold The datatype defines the type of data a variable or actionscript element can hold Primitive datatypes: Primitive datatypes:  String, Number, Boolean, (undefined, null) Complex datatypes: Complex datatypes:  Object, MovieClip, Void Void: indicates that a function does not return a value

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

5 Datatype: String Strings are sequences of characters, numbers and punctuation marks. These are enclosed within single (') or double (") quotation marks Strings are sequences of characters, numbers and punctuation marks. These are enclosed within single (') or double (") quotation marks//declaration var myURL_string:String; //assignment myURL_string = "www.flashkit.com";

6 String functions var my_string:String; //concatenation my_string = ”Hi ”+”there!”; //traces to Hi there! //getting substring var sub_string:String = my_string.substring(3, my_string.length); //making all characters uppercase var upper_string:String = sub_string.toUpperCase(); trace(upper_string);//yields THERE!

7 Datatype: Number Numbers represent numerical values, both integers and floats Numbers represent numerical values, both integers and floats//declaration var length:Number; //assignment length = 1100; length = -22 length = 0.00002234 length = 100/3; //traces to 33.3333333333333 length = 1/0; //traces to Infinity x = 5; y = ”56” //from a text z = x + y; //(z = ”556”) z = x + Number(y); //(z = 61)

8 Number Variable declaration, assignment and initialization Variable declaration, assignment and initialization//declaration var height:Number; //assignment height = 200;//literal value height = anotherVariable;//value from another variable //initialization(declaration and assignment on the //same code line) var width:Number = 300;

9 Datatype: Boolean Boolean represents a boolean value, possible values: true or false Boolean represents a boolean value, possible values: true or false Converts the parameter expression to a Boolean value and returns true or false Converts the parameter expression to a Boolean value and returns true or false var isLoaded:Boolean; isLoaded = true;

10 Array Arrays are lists of data under which each item is identified by its order within the list Arrays are lists of data under which each item is identified by its order within the list An array can be made up of primitive type values like strings, numeric values, booleans or complex type values like other arrays or objects An array can be made up of primitive type values like strings, numeric values, booleans or complex type values like other arrays or objects music_array = new Array(); music_array = [“Metallica”, “Bruce Springsteen”, “U2”, “Iron Maiden”, “David Gray”, “Van Morrison”]; music_array.length // traces the lenght of the array music_array[1]; // traces Bruce Springsteen music_array.slice(2,4); //traces U2, Iron Maiden

11 Object A collection of properties that describes the object A collection of properties that describes the object Ex. An apple has properties like smell, color, size and position The object can contain different datatypes The object can contain different datatypes Positve: Return the Object (all properties) in one call Positve: Return the Object (all properties) in one call Example: How to create your own object: var user:Object = new Object(); user.name = "Irving"; user.age = 32; user.hobby = “Drinking!";

12 Arithmetic operators Arithmetic operators Arithmetic operators +, -, *, /, % ++, - - +=, -=, *=, /=, %= Increment ++, and decrement - - Increment ++, and decrement - - Increments/decrements a variable by 1 var x:Number = 10; x++; trace(x);//yields 11 x--; trace(x);//yields 10

13 Precedence The answer depends on operator precedence The answer depends on operator precedence var i:Number; i = 12 + 3 * 10 / 2; //traces 25 You can override precedence by using parenteses You can override precedence by using parenteses var i:Number; i=(12 + 3) * 10 / 2; trace(i);//yields 75

14 Variables & Text (1/2) Static Text: Animations, common use, standard fonts Static Text: Animations, common use, standard fonts Input Text: Inputtext, forms, variables etc Input Text: Inputtext, forms, variables etc Dynamic Text: Dynamic text, variable in & output, non-standard fonts (embed fonts), scrolling text, loading of text (html-data) Dynamic Text: Dynamic text, variable in & output, non-standard fonts (embed fonts), scrolling text, loading of text (html-data) for example by :  Textfil  XML  Databas

15 Variables & Text (1/2)  An Input- and/or a dynamic textfield/box, could be connected to a variable ( my_variable )  These textfields could also have an instance name ( my_txt )

16 Variables and Buttons Example: A button, input textfield and output field Example: A button, input textfield and output field //declare variables var input_string:String = ""; var output_string:String = ""; //button/function for output my_btn.onPress = function(){ output_string = input_string; }


Download ppt "Variables & datatypes Flash ActionScript Introduction to."

Similar presentations


Ads by Google