Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Similar presentations


Presentation on theme: "Lights, Camera, ActionScripts Web Design ½ Shade Adetoro."— Presentation transcript:

1 Lights, Camera, ActionScripts Web Design ½ Shade Adetoro

2 What are Variables? Variables are like boxes and/a container –It contains things. –It stores things For Actionscript, it will have a name and contain values and objects A box can contain different types of things, so usually you start by identifying the type of value a variable contains.

3 What Variables contain A variable can be local or global: –Local: If the variable is defined into a function or a loop, one the function is carried out, the varible will be automatically deleted from Flash’s memory. –Global: This variable is always kept in memory, and is always accessible.

4 Declaring a Variable var points:Number = 10; points+=10; trace (points); var declare the variable to Flash miles is the name of the variable :Number; is the type of value that the variable can contain. A numerical value in this case.

5 What da? What does this mean? –You are telling Flash to create a box with a label name (miles) and also specify to Flash that the box can only contain values of numerical type such as 0, 3, 124, 0.4, 654.332… –Next assign a variable to flash miles=10; var miles:Number=10;

6 continued Little parenthesis: :Number is define as the Data Type of the variable. Next, you type: trace(miles); Here you tell Flash to show you what is in the box, so flash will open the box and will show you the value contained into it. It obtains the following output: 10

7 Error Code:miles='Davis'; if I publish the SWF, Flash would return the following error: 1158/1067: Implicit coercion of a value of type String to an unrelated type Number. I tells me that the type of value is wrong and that I am trying to assign a String value to a variable data typed Number:

8 Replace a variable Instead, if after writing var miles:Number=10; I would add: Code: var miles:Number=10; miles=15; with a trace(miles), I would obtain the following output: 15 We can understand now that we can change the value of the variable as wanted keeping in mind to always use the same data type declared

9 Adding variables With the following example, I add a value to the existing value of miles: Code: miles+=10; this syntax tells Flash: open the box named miles, take its value, add 10 and close again the box. Therefore, it adds a value of 10 to the already existing value. In fact with trace (miles); I get the following output: 20

10 Types of Variables Which types of values can I assign to a variable? Answer: every type of value of the built-in classes that exist in Flash, therefore Number, String, MovieClip, TextField, etc. A complete list of the existing classes in

11 List of the Value classes Can be found at http://livedocs.adobe.com/flash/9.0/Action ScriptLangRefV3/migration.html

12 A few examples Create a variable of type String and I assign to it a string value: line 1: var my_name:String=“Shade"; line 2: trace(my_name); You should obtain the following output: Shade it is important to write the string (as it is a string) in between the double quote “Shade” otherwise if you type write: Code: var my_name:String=filippo; you would get an error

13 Text Variable Another example, I create a variable a type textField: Line 1: var field:TextField=new TextField(); once again, we find the operator new.

14 Movie Clips Another example, I create a variable of type MovieClip: Line 1: var clip:MovieClip=new MovieClip(); This operator allows Flash to understand that it has to create a new MovieClip as the variable clip will contain a MovieClip. So, using var clip:MovieClip=new MovieClip(); we are telling Flash to create a new empty MovieClip.

15 Name Variable var my_name:String="Shade"; trace(my_name);

16 Movie +Text var field:TextField=new TextField(); trace(TextField); var clip:MovieClip=new MovieClip(); trace (MovieClip);

17 Conversation w/Flash var message = “Hi Flash”; var firstName = “Shade”; trace (message); trace (“Hi Flash”, “shade”);

18 Comments Comments are helpful lines that are not compiled. They help readers understand our program, Summarize a function's algorithm, Identify the purpose of a variable, or Clarify a segment of code.

19 There are 2 types of Comments Multiple Lines ( /* */ ) /* An explanation of a section of code or a coder's name and date and other stuff */ OR Single Line delimiters (//) // number of record var recordNum ;

20 Operators Operators are signs that do something to variables on either side of them.Here are the arithmetic operators: OperatorName Example * Multiplication X* Y / Division X/Y % Modulus X % Y + Addition X + Y - Subtraction X - Y The Modulus operator(%) computes the remainder of division between 2 integers. e.g. 5 % 2 = 1 5/2 = (2*2) + 1(remainder) Ex. 1 var x = 3; var y = 5; var mult1 = x * y; trace(x + " times " + y + " = " + mult1);

21 Operators continued….. ex2 var min = 35; var max = 239; var addition = min + max; trace(min + " plus " + max + " = " + addition;

22 Equality, Relational and Logical Operators These operators evaluate to true or false Logical AND (&&) Evaluates to true only if both its operands evaluate to true. E.g. x = 5; if(x>0 && x < 10){ something = true; } trace(something);

23 Operator Name Example ! Logical NOT (!hasFired) <Less than x < y >Greater thanx> y <=Less than or equal to x <= y >=Greater than or equal to x >= 24 ==Equality if(hasFired == true) !=Inequality If( x != 7) &&Logical AND x >0 && x < 10 ||Logical OR x 20

24 Assignment Operator ( = ) The effect that an assignment has is to store a new value in the left operand's associated memory storage space e.g. x = 7; This statement means that the value 7 is assigned to the variable x.

25 Increment and decrement Operators ( ++ and -- ) The increment (++) and decrement (--) operators are a shortcut way of adding or subtracting 1 from a variable. E.g. Prefix increment - ++c; // c = c+1 Postfix increment - c++; The prefix form of ++ increments the value of the variable before that value is used. stack[++top] = val; //Is equivalent to the following 2 lines top = top +1; stack[top] = val; The postfix form of ( --) decrements the value of top after that value is used. stack[top--] = val; //Is equivalent to : stack[top] = v; top = top -1;

26 “IF” operator Arithmetic "IF" operator The syntactic form is: Expression1 ? expr2 : expr3 ; If expression1 evaluates to a true condition, then expression2 is evaluated, otherwise expression3 is evaluated. E.g. x = 2; y = 5; max = x<y ? 100 : 10; trace(max);

27 Right/Left Shift Left Shift ("<<") - shifts the bits of the left operand some number of positions to the left. e.g. someNum = 1; 0 0 0 0 0 0 0 1 SomeNum = someNum >") SomeNum = someNum >>3; 0 0 0 0 0 0 0 1 The leftShift operator inserts "0" from the right. The Right Shift operator inserts "0" from the left.

28 More about variables A variable is identified by a user-supplied name. Each variable is of a particular data type. E.g. Var num:Number; "Number" is a type specifier. A declaration can follow the template: var variableName:DataType; The use of the ":Datatype" is not strictly enforced in AS2 but I strongly recommend it. A variable can be composed of letters, numbers and underscore. Upper and lower case letters are different. Variables need to be initialised. I.e. given an initial value. var myNum:Number; //Declaration myNum = 0; //Initialisation

29 Compiler Errors Syntax errors - e.g. var numberOne: Spelling Mistakes - var mc:MoveiClip; DataType Errors - (AS 2.0)

30 Flow control IF An if statement tests a condition. If it is true, then a set of actions is executed. Otherwise, the set is ignored or bypassed. Here is a template : If(expression){ Statement1; } Here is an example : var x = 23; if(x>3){ trace("x is greater than 3"); }


Download ppt "Lights, Camera, ActionScripts Web Design ½ Shade Adetoro."

Similar presentations


Ads by Google