Actionscript - Introduktion
Download
Report
Transcript Actionscript - Introduktion
Introduction to
Flash ActionScript
Variables & datatypes
Variables
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
The datatype defines the type of data a variable or actionscript
element can hold
Primitive datatypes:
String, Number, Boolean, (undefined, null)
Complex datatypes:
Object, MovieClip, Void
Void: indicates that a function does not return a value
Naming variables
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 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 xSpeed:Number;
Datatype: String
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";
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!
Datatype: Number
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)
Number
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;
Datatype: Boolean
Boolean represents a boolean value, possible values: true or false
Converts the parameter expression to a Boolean value and returns
true or false
var isLoaded:Boolean;
isLoaded = true;
Array
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 = 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
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
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!";
Arithmetic operators
Arithmetic operators
+, -, *, /, %
++, - +=, -=, *=, /=, %=
Increment ++, and decrement - Increments/decrements a variable by 1
var x:Number = 10;
x++;
trace(x);//yields 11
x--;
trace(x);//yields 10
Precedence
The answer depends on operator precedence
var i:Number;
i = 12 + 3 * 10 / 2; //traces 25
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, non-standard
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
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;
}