Introduction to Flash Action Script Variables datatypes Variables
Introduction to Flash Action. Script Variables & datatypes
Variables n n n A variable is a place to store information It has a name and a type Variables are used to make the code dynamic Tip: Declare your variables first (on top)
Datatypes n The datatype defines the type of data a variable or actionscript element can hold n Primitive datatypes: § String, Number, Boolean, (undefined, null) n Complex datatypes: § Object, Movie. Clip, Void: indicates that a function does not return a value
Naming variables n n n n Variable names can only contain letters, numbers, and dollar signs ($) All variables must have unique names Start variables with a lowercase letter Use mixed case for concatenated words Don't use reserved words: this, menu, private, video, etc. Variables are case-sensitive Don't use the same variable name with different cases Keep variables as short as possible while retaining clarity Example, with strict datatyping: var x. Speed: Number;
Datatype: String n Strings are sequences of characters, numbers and punctuation marks. These are enclosed within single (') or double (") quotation marks //declaration var my. URL_string: String; //assignment my. URL_string = "www. flashkit. com";
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. to. Upper. Case(); trace(upper_string); //yields THERE!
Datatype: Number n 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. 3333333 length = 1/0; //traces to Infinity x = 5; y = ” 56” //from a text z = x + y; //(z = ” 556”) z = x + Number(y); //(z = 61)
Number n Variable declaration, assignment and initialization //declaration var height: Number; //assignment height = 200; //literal value height = another. Variable; //value from another variable //initialization(declaration and assignment on the //same code line) var width: Number = 300;
Datatype: Boolean n n Boolean represents a boolean value, possible values: true or false Converts the parameter expression to a Boolean value and returns true or false var is. Loaded: Boolean; is. Loaded = true;
Array n n 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 music_array Maiden”, = new Array(); = [“Metallica”, “Bruce Springsteen”, “U 2”, “Iron “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 U 2, Iron Maiden
Object A collection of properties that describes the object Ex. An apple has properties like smell, color, size and position n The object can contain different datatypes n Positve: Return the Object (all properties) in one call n Example: How to create your own object: var user: Object = new Object(); user. name = "Irving"; user. age = 32; user. hobby = “Drinking!";
Arithmetic operators +, -, *, /, % ++, - +=, -=, *=, /=, %= n Increment ++, and decrement - Increments/decrements a variable by 1 n var x: Number = 10; x++; trace(x); //yields 11 x--; trace(x); //yields 10
Precedence n The answer depends on operator precedence var i: Number; i = 12 + 3 * 10 / 2; //traces 25 n You can override precedence by using parenteses var i: Number; i=(12 + 3) * 10 / 2; trace(i); //yields 75
Variables & Text (1/2) • Static Text: Animations, common use, standard fonts • Input Text: Inputtext, forms, variables etc • Dynamic Text: Dynamic text, variable in & output, nonstandard fonts (embed fonts), scrolling text, loading of text (html-data) for example by : § § § Textfil XML Databas
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)
Variables and Buttons n Example: A button, input textfield and output field //declare variables var input_string: String = ""; var output_string: String = ""; //button/function for output my_btn. on. Press = function(){ output_string = input_string; }
- Slides: 16