+ All Categories
Home > Documents > Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Date post: 26-Dec-2015
Category:
Upload: shanon-warner
View: 218 times
Download: 2 times
Share this document with a friend
Popular Tags:
30
Lights, Camera, ActionScripts Web Design ½ Shade Adetoro
Transcript
Page 1: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Lights, Camera, ActionScripts

Web Design ½

Shade Adetoro

Page 2: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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.

Page 3: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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.

Page 4: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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.

Page 5: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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;

Page 6: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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

Page 7: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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:

Page 8: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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

Page 9: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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

Page 10: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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

Page 11: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

List of the Value classes

• Can be found at

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/migration.html

Page 12: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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

Page 13: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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.

Page 14: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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.

Page 15: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Name Variable

• var my_name:String="Shade";

• trace(my_name);

Page 16: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Movie +Text

• var field:TextField=new TextField();

• trace(TextField);

• var clip:MovieClip=new MovieClip();

• trace (MovieClip);

Page 17: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Conversation w/Flash

var message = “Hi Flash”;

var firstName = “Shade”;

trace (message);

trace (“Hi Flash”, “shade”);

Page 18: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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.

Page 19: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

There are 2 types of Comments

• Multiple Lines ( /* */ )

/*An explanation of a section of code ora coder's name and dateand other stuff*/

OR

•Single Line delimiters (//)

// number of recordvar recordNum ;

Page 20: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

OperatorsOperators 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 = 15/2 = (2*2) + 1(remainder)

• Ex. 1var x = 3;var y = 5;var mult1 = x * y;trace(x + " times " + y + " = " + mult1);

Page 21: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Operators continued…..

•ex2

var min = 35;var max = 239;var addition = min + max;trace(min + " plus " + max + " = " + addition;

Page 22: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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);

Page 23: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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< 0 || x > 20

Page 24: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Assignment Operator ( = )

• 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.

Page 25: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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 linestop = 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;

Page 26: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

“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);

Page 27: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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<<1;0 0 0 0 0 0 1 0

SomeNum = someNum<<2;0 0 0 0 1 0 0 0

Right Shift (">>")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.

Page 28: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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

Page 29: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

Compiler Errors

• Syntax errors - e.g. var numberOne:

• Spelling Mistakes - var mc:MoveiClip;

• DataType Errors - (AS 2.0)

Page 30: Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.

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");}


Recommended