Upload
erika-mcbride
View
233
Download
0
Embed Size (px)
Citation preview
Variables & Data Variables & Data typestypes
Flash ActionScript Flash ActionScript 3.03.0
Introduction toIntroduction to
Thomas Lövgren, Flash developer
Variables A variable is a place to store informationA variable is a place to store information It has a name and a typeIt has a name and a type Variables are used to make the code dynamicVariables are used to make the code dynamic Values can generally be accessed or changed at any timeValues can generally be accessed or changed at any time An identifier (usually a letter, word, or phrase) that is An identifier (usually a letter, word, or phrase) that is
linked to a value stored in the system's memory or an linked to a value stored in the system's memory or an expression that can be evaluatedexpression that can be evaluated
Depending on the type system of a programming Depending on the type system of a programming language, variables may only be able to store a specified language, variables may only be able to store a specified data typedata type
Naming variablesNaming variables Variable names can only contain letters, numbers, Variable names can only contain letters, numbers,
and dollar signs ($)and dollar signs ($) All variables must have unique namesAll variables must have unique names Start variables with a lowercase letterStart variables with a lowercase letter Variables are Variables are case-sensitivecase-sensitive Use mixed case for concatenated wordsUse mixed case for concatenated words Don't use reserved words: Don't use reserved words: this, menu, private, video, this, menu, private, video,
etc.etc. Don't use the same variable name with different Don't use the same variable name with different
casescases Keep variables as short as possible while retaining Keep variables as short as possible while retaining
clarityclarity
Example, with strict datatypingExample, with strict datatyping::var xSpeed:Number;var xSpeed:Number;
Data typesData types The The Data type Data type defines the type of data a variable defines the type of data a variable
or ActionScript element can holdor ActionScript element can hold
Primitive data types (Primitive data types (Top level data typesTop level data types): : Boolean, int, Null, Number, String, uint, Boolean, int, Null, Number, String, uint,
and voidand void
Complex data types: Complex data types: Object, Array, Date, Error, Function, Object, Array, Date, Error, Function,
RegExp, XML, and XMLListRegExp, XML, and XMLList
Variable syntaxVariable syntax Example of the different parts and structure of a Example of the different parts and structure of a
variable declaration/populationvariable declaration/population
Data type: String (1/2)Data type: String (1/2) Strings are sequences of characters, numbers and Strings are sequences of characters, numbers and
punctuation marks. These are enclosed within punctuation marks. These are enclosed within double (") quotation marksdouble (") quotation marks
The String data type represents a sequence of 16-bit The String data type represents a sequence of 16-bit characterscharacters
The default value for a variable declared with the The default value for a variable declared with the String data type is nullString data type is null
//declaration//declarationvar myURL_string:String;var myURL_string:String;
//assignment//assignmentmyURL_string = "www.flashkit.com";myURL_string = "www.flashkit.com";
//declaration and assignment//declaration and assignmentvar myURL_string:String = "www.flashkit.com";var myURL_string:String = "www.flashkit.com";
Example of some methods for the Example of some methods for the String objectString object are: are:Substring, charAt, replace, toUpperCase, split, Substring, charAt, replace, toUpperCase, split, joinjoin etc etc
//declare variable//declare variablevar my_string:String;var my_string:String;
//assignment and concatenation//assignment and concatenationmy_stringmy_string = ”Hi ”+”there!”; = ”Hi ”+”there!”; //traces to Hi there!//traces to Hi there!
//getting substring//getting substringvar sub_string:String = var sub_string:String = my_stringmy_string.substring(3, .substring(3, my_stringmy_string.length);.length);
//making all characters uppercase//making all characters uppercasevar upper_string:String = sub_string.toUpperCase();var upper_string:String = sub_string.toUpperCase();trace(upper_string); trace(upper_string); //yields THERE!//yields THERE!
Tip! Declare your variables first (on top)Tip! Declare your variables first (on top)
Datatype: String (2/2)Datatype: String (2/2)
Data type: Number (1/2)Data type: Number (1/2) The Number data type can represent integers, The Number data type can represent integers,
unsigned integers, and floating-point numbersunsigned integers, and floating-point numbers The Number data type uses the 64-bit double-The Number data type uses the 64-bit double-
precision formatprecision format The default value is NaNThe default value is NaN
//declaration//declarationvar length:Number;var length:Number;
//assignment//assignmentlength = 1100;length = 1100;length = -22;length = -22;length = 0.00002234;length = 0.00002234;length = 100/3; length = 100/3; //traces to 33.3333333333333//traces to 33.3333333333333length = 1/0; length = 1/0; //traces to Infinity//traces to Infinity
Variable declaration, assignment and initializationVariable declaration, assignment and initialization
//declaration//declarationvar height:Number;var height:Number;
//assignment//assignmentheight = 200;//literal valueheight = 200;//literal valueheight = anotherVariable;//value from another variableheight = anotherVariable;//value from another variable
//initialization(declaration and assignment on the same code line)//initialization(declaration and assignment on the same code line)var width:Number = 300;var width:Number = 300;
//check max and min values//check max and min values Number.MAX_VALUE == 1.79769313486231e+308Number.MAX_VALUE == 1.79769313486231e+308Number.MIN_VALUE == 4.940656458412467e-324Number.MIN_VALUE == 4.940656458412467e-324
Data type: Number (2/2)Data type: Number (2/2)
Data type: intData type: int New data type in AS3New data type in AS3 The The intint data type is a 32-bit integer between - data type is a 32-bit integer between -
2,147,483,648 and 2,147,483,6472,147,483,648 and 2,147,483,647 Integers only work in whole numbers and ignore the Integers only work in whole numbers and ignore the
decimal value, always rounding downdecimal value, always rounding down The default value for variables that are of data type The default value for variables that are of data type
uint is 0uint is 0
//example 1//example 1
var myInt:int = -15;var myInt:int = -15;
var myInt2:int = 3500;var myInt2:int = 3500;
//example 2//example 2
var num:int = 2.5;var num:int = 2.5;
var product:int = num + 2;var product:int = num + 2;
trace(product); trace(product); //4, the decimal will be ignored//4, the decimal will be ignored
Data type: unitData type: unit New data type in AS3New data type in AS3 The The uintuint (Unsigned Integer) data type is a 32-bit (Unsigned Integer) data type is a 32-bit
unsigned integer between 0 and 4,294,967,295unsigned integer between 0 and 4,294,967,295 Unsigned integer or any non-negative whole Unsigned integer or any non-negative whole
numbernumber The default value for variables that are of data The default value for variables that are of data
type uint is 0type uint is 0
var myUnit:unit = 1;var myUnit:unit = 1;
var mySecondUnit:unit = 3500;var mySecondUnit:unit = 3500;
var unsignedInteger:uint; var unsignedInteger:uint; //0//0
Number, int or unit?Number, int or unit?
To maximize performance, it’s recommended that we use the To maximize performance, it’s recommended that we use the Number data type only for integer values Number data type only for integer values largerlarger than the int than the int and uint types can store or for floating-point numbers. and uint types can store or for floating-point numbers.
To store a floating-point number, include a decimal point in To store a floating-point number, include a decimal point in the number the number
If we omit a decimal point, the number will be stored as an If we omit a decimal point, the number will be stored as an integerinteger
Data type: BooleanData type: Boolean Boolean represents a boolean value, possible Boolean represents a boolean value, possible
values: values: truetrue or or falsefalse Converts the parameter expression to a Boolean Converts the parameter expression to a Boolean
value and returns true or falsevalue and returns true or false Default vaule is Default vaule is falsefalse
//declaration of a boolean variable//declaration of a boolean variable
var isLoaded:Boolean;var isLoaded:Boolean;
//assignmet//assignmet
isLoadedisLoaded = true; = true;
Default Values (1/2)Default Values (1/2) A A Default value Default value is the value that a variable is the value that a variable
contains before you set its valuecontains before you set its value You You initialize initialize a variable when you set its value a variable when you set its value
for the first timefor the first time If you declare a variable, but do not set its value, If you declare a variable, but do not set its value,
that variable is that variable is uninitializeduninitialized The value of an uninitialized variable depends on The value of an uninitialized variable depends on
its data typeits data type
Default Values (2/2)Default Values (2/2) The following table describes the default values of The following table describes the default values of
variables, organized by data typevariables, organized by data type
NoteNote! For variables of type Number, the default value is NaN (not a ! For variables of type Number, the default value is NaN (not a number)number)
Array (1/2)Array (1/2) Arrays are lists of data under which each item is Arrays are lists of data under which each item is
identified by its order within the listidentified by its order within the list The first element in an Array has index 0The first element in an Array has index 0 An array can be made up of primitive type values An array can be made up of primitive type values
like strings, numeric values, booleans or complex like strings, numeric values, booleans or complex type values like other arrays or objectstype values like other arrays or objects
var music_array:Array = new Array();var music_array:Array = new Array();
music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron Maiden", "David Gray", "Van Morrison"];Maiden", "David Gray", "Van Morrison"];
music_array.length music_array.length //traces the length of the array//traces the length of the array
music_array[1]; music_array[1]; //traces Bruce Springsteen //traces Bruce Springsteen
var my_array:Array = [5,"Hello!",{a:5, b:7}]; var my_array:Array = [5,"Hello!",{a:5, b:7}]; //complex array//complex array
Array (2/2)Array (2/2) Example of some methods for the Example of some methods for the Array objectArray object are: are:
slice, join, concat, push, pop, reverseslice, join, concat, push, pop, reverse etc. etc.
var music_array:Array = new Array();var music_array:Array = new Array();
music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron music_array = ["Metallica", "Bruce Springsteen", "U2", "Iron Maiden", "David Gray", "Van Morrison"];Maiden", "David Gray", "Van Morrison"];
//slice(startIndex, endIndex)//slice(startIndex, endIndex)
music_array.slice(2,4); music_array.slice(2,4); //traces U2, Iron Maiden//traces U2, Iron Maiden
TypecastingTypecasting Sometimes we need to Sometimes we need to typecast typecast a data type into a data type into
another (for some reson), for exampe if we load another (for some reson), for exampe if we load XML data (numbers) and Flash interpret it like XML data (numbers) and Flash interpret it like string vaulesstring vaules
Here’s an example of how we can typecast a string Here’s an example of how we can typecast a string into a numberinto a number
var my_string:String = "50";var my_string:String = "50";
var my_num:Number = 20;var my_num:Number = 20;
var answer:Number;var answer:Number;
trace(my_string + my_num); trace(my_string + my_num); //traces 5020//traces 5020
//typecast the string to a number data type//typecast the string to a number data type
trace(Number(my_string) + my_num); trace(Number(my_string) + my_num); //traces 70//traces 70
ScopeScope Scope is the realm or space in wich an object Scope is the realm or space in wich an object
(variable) lives(variable) lives In ActionScript 3.0, variables are always assigned In ActionScript 3.0, variables are always assigned
the scope of the function or class in which they are the scope of the function or class in which they are declareddeclared
Entrance into that scope typically begins a variable's Entrance into that scope typically begins a variable's lifetime and exit from that scope typically ends its lifetime and exit from that scope typically ends its lifetimelifetime
function localScope(){ function localScope(){
var strLocal:String = "local"; var strLocal:String = "local";
} }
localScope(); localScope();
trace(strLocal); trace(strLocal); //error because strLocal is not defined globally//error because strLocal is not defined globally
Arithmetic operatorsArithmetic operators Arithmetic operatorsArithmetic operators
+, -, *, /, %+, -, *, /, %
++, - -++, - -
+=, -=, *=, /=, %=+=, -=, *=, /=, %=
Increment ++, and decrement - - Increment ++, and decrement - -
Increments/decrements a variable by 1Increments/decrements a variable by 1var x:Number = 10;var x:Number = 10;
x++;x++;
trace(x); trace(x); //traces 11//traces 11
x--;x--;
trace(x); trace(x); //traces 10//traces 10
PrecedencePrecedence The answer depends on operator precedenceThe answer depends on operator precedence
var i:Number;var i:Number;
i = 12 + 3 * 10 / 2; i = 12 + 3 * 10 / 2; //traces 27//traces 27
You can override precedence by using parentesesYou can override precedence by using parenteses
var i:Number;var i:Number;
i=(12 + 3) * 10 / 2;i=(12 + 3) * 10 / 2;
trace(i); trace(i); //traces 75//traces 75