Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer

Similar presentations


Presentation on theme: "Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer"— Presentation transcript:

1 Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer thomas.lovgren@humlab.umu.se

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 Values can generally be accessed or changed at any time 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 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 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 ($) 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 Variables are case-sensitive Variables are case-sensitive 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. 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;

4 Data types The Data type defines the type of data a variable or ActionScript element can hold The Data type defines the type of data a variable or ActionScript element can hold Primitive data types (Top level data types: Primitive data types (Top level data types):  Boolean, int, Null, Number, String, uint, and void Complex data types: 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 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 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 String data type represents a sequence of 16-bit characters The default value for a variable declared with the String data type is null The default value for a variable declared with the String data type is null//declaration var myURL_string:String; //assignment myURL_string = "www.flashkit.com"; //declaration and assignment var myURL_string:String = "www.flashkit.com";

7 Example of some methods for the String object are: Example of some methods for the String object are: Substring, charAt, replace, toUpperCase, split, join etc //declare variable var my_string:String; //assignment and 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!  Tip! Declare your variables first (on top) Datatype: String (2/2)

8 Data type: Number (1/2) The Number data type can represent integers, unsigned integers, and floating-point numbers 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 Number data type uses the 64-bit double-precision format The default value is NaN The default value is NaN//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

9 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; //check max and min values Number.MAX_VALUE == 1.79769313486231e+308 Number.MIN_VALUE == 4.940656458412467e-324 Data type: Number (2/2)

10 Data type: int New data type in AS3 New data type in AS3 The int data type is a 32-bit integer between -2,147,483,648 and 2,147,483,647 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 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 The default value for variables that are of data type uint is 0 //example 1 var myInt:int = -15; var myInt2:int = 3500; //example 2 var num:int = 2.5; var product:int = num + 2; trace(product); //4, the decimal will be ignored

11 Data type: unit New data type in AS3 New data type in AS3 The uint (Unsigned Integer) data type is a 32-bit unsigned integer between 0 and 4,294,967,295 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 Unsigned integer or any non-negative whole number The default value for variables that are of data type uint is 0 The default value for variables that are of data type uint is 0 var myUnit:unit = 1; var mySecondUnit:unit = 3500; var unsignedInteger:uint; //0

12 Number, int or unit? To maximize performance, it’s 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 maximize performance, it’s 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 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 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 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 Default vaule is false Default vaule is false //declaration of a boolean variable var isLoaded:Boolean; //assignmet isLoaded = true;

14 Default Values (1/2) A Default value is the value that a variable contains before you set its value 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 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 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 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 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) 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 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 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 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_array:Array = new Array(); music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron Maiden", "David Gray", "Van Morrison"]; music_array.length //traces the length of the array music_array[1]; //traces Bruce Springsteen var my_array:Array = [5,"Hello!",{a:5, b:7}]; //complex array

17 Array (2/2) Example of some methods for the Array object are: Example of some methods for the Array object are: slice, join, concat, push, pop, reverse etc. var music_array:Array = 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 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 Here’s an example of how we can typecast a string into a number Here’s an example of how we can typecast a string into a number var my_string:String = "50"; var my_num:Number = 20; var answer:Number; 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 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 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 Entrance into that scope typically begins a variable's lifetime and exit from that scope typically ends its lifetime function localScope(){ var strLocal:String = "local"; var strLocal:String = "local";}localScope(); trace(strLocal); //error because strLocal is not defined globally

20 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); //traces 11 x--; trace(x); //traces 10

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


Download ppt "Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer"

Similar presentations


Ads by Google