Upload
nafis-ahmed
View
259
Download
1
Embed Size (px)
DESCRIPTION
In this chapter, I have discussed about the language basics that creates the foundation of Adobe Actionscript 3. From variables, data types, comments, loops to conditionals, much of the basics are covered about flash actionscript in this chapter. In the next chapter, chapter-3, I will be discussing about creating classes and making objects out of it. So tighten your seat bealts buddies! Visit my youtube channel Nafis's Classroom to watch the entire lesson: https://www.youtube.com/channel/UCD1a7Kgz4m8tp-U6T8dLyMg
Citation preview
Overview : What is ActionScript• An object-oriented programming language used to build Flash
content • Developed by Adobe and recent version is ActionScript 3.0• Can also be used to create APIs that are presentations, games,
toys, websites or experiments• Enables the ActionScript programmer to draw
programmatically• Has similarities with Ajax or JavaScript because it relies on
DOM (Document Object Model) and responds to clicks or mouse events, for example.
Remem
ber:
ActionScrip
t
and Ajax is
not the sa
me
thing!
TIME FOR SOME BASICS!
Variables, constants, scopes, operators, conditional statements, loops and comments
• They are temporary storage systems that hold string of characters, numbers or other values
• They contain information that are called objects when the program is running
• They are also known as properties when they are a part of a class
• Usage of underscores, numbers and letters are allowed
• To declare a variable and assigning a value: var city:String; city = “dhaka”;Or var city:String = “dhaka”;
Several declaration at once
var city:String, country:String, myName:String;
• Camel case should be used when naming variables ex: myName = “whatever”
Variables / Storage System
Constants
• A special kind of variable that never changes when the program is running
• Produces an error when an attempt is made to change the constant’s value
• Constant’s name should be all uppercase letters (recommended)
• Can be declared at the beginning of a class or in the constructor for the class
• To declare a constant: const DAYS:int = 7;
Be Literal Buddy! (Literals)• Literal means the value that
is explicitly included in the code when it’s compiled
• Beware: use of literals is not recommended ‘cause they can be hard to track when program needs to be debugged
• Regular expressions with forward slashes like /href=“#”/
• XML elements like <location><city
name=“Dhaka”/></location>
• Generic objects like {city: “Dhaka”, name:
“Nafis”}
• Array literals like [“a”, “b”, “c”]
• Empty values like void, undefined or null
• Strings with double or single quotes Like ‘literals’
• Boolean values Like true and false
• Numbers of type int or uint like 100, 3.22
Access Controls• Written before a
property/variable or a method/class-function
• Four types of access control attributes: public private internal protected
• To use a access control keyword: private var name:String = “Nafis”; public class anything {
//code goes here
}
• Public: Any code can access the property or the method it marked with this keyword
• Private: Property or method named private can only be accessed within a class
• Internal: Neither public nor private but internal objects are accessible to classes within the same class package/ any class that has the same package declaration This is the default access control for all
objects in AS 3.0
• Protected: Protected objects can be accessed by child or subclasses/ when a class is extended
Introduction to Scope
• Variables and functions in AS exist inside a specified scope • Two types of scope
– Global scope: Any code defined outside a class or function will be regarded as in the global scope and can be accessed from anywhere
– Local scope: Objects inside any class or function can be accessed only in their given scope/space not from everywhere. Local scope has numerous layers
Remember:
Try to be local
buddy!
Layer’s of Local Scope• Class level/Static variables and
methods exist in the class itself, not requiring an instantiation of the class
• Instance level variables & functions exists in every single instance of a class
• Objects defined in functions that are housed inside classes are only available temporarily
– Variables or properties inside the class is available to methods or functions
– Functions’ parameters exists only inside the scope of the function
– Objects in more localized scopes will override objects in more global scopes
• Ex: public class scope1 { public static var name:String = “Nafis”;
}• To create an instance of a class:
var newS:scope1 = new scope1();
• When static keyword is used: trace(scope1.name); // produces: Nafis
• To access an instance variable:– public class scope2 {
public var postalCode:Number = 1200; }– var newS2:scope2 = new scope2();– trace(newS2.postalCode); // outputs
1200
• Scope inside a function:– public class scope3 {
public function hello():void { var msg:String = “What’s Up!”; trace(msg);}
}– var newS3:scope3 = scope3();– newS3.hello(); // outputs: What’s Up!– trace(newS3.hello); // undefined
Explaining Data Types• A data type refers to the data that
can be stored in the object for example
• Every object/variable or function’s parameter or return type need to have a data type
• Objects with unknown type can have a wildcard(*) data type
• Usage of colon operator is compulsory when declaring a data type of a variable
– var months:Number;– No problem!
• x = 22;• x = -33;• x = 3.3;
– Problems…..Errrr…• x = “cat”;• x = “dog”;
• To assign a wildcard data type:– var unknown:* = “any value”;
Introduction to Operators• They are built-in commands used for operating on one or more values• Three types of operators:
– Unary: Requires single argument to operate, like decrement operator (--)– Binary: Requires two arguments to operate, like arithmetic's addition operator (+)– Trinary: Requires three arguments to evaluate (?:)
• Operator orders:• Functional results are evaluated after their arguments are evaluated• Expressions that are more deeply nested are executed first, like: ((2-3)+6)*4 = 20• Multiplication, division and modulus operators are given equal significance (*, /, %) and
are evaluated from left to right• Addition and subtraction have equal importance (+, -) and are evaluated from left to
right
Let’s Meet the Operators!• The commonly used arithmetic
operators– + Addition– - Subtraction– * multiplication– / division
• % modulo operator returns the leftover or the remainder after dividing two numbers like 4 % 3 = 1
• Compound assignment operators perform arithmetic operation on numbers and reassign the answer to the number, like: x += 1, which is also x = x + 1
• Compound assignment operators are
– += Add and reassign– *= Multiply and reassign– -= Subtract and reassign– /= Divide and reassign– %= Get the remainder from division
and reassign it to the variable
Conditionals• Conditionals help to produce different outcomes for varied circumstances
by evaluating the truth of logical expression• Every conditional statement relies on Boolean operation. Booleans are
objects that can either have the value of true or false• Conditionals include
– If / else if / else statements– If statements rely on several comparison operators– Switch / case / default statements
If / else if / else• If statements only evaluates
when a certain/given condition is true
• Additional if statements can be added with else if if the first condition in the first statement is not true
• Lastly, an else statement can be added as a default block if all the other if statements are evaluated to be false
• var today:String = “Saturday”;
• Format of if statement– if (logical expression) { //code to run if condition is met } else if (logical expression) { //code to run if the first condition is false } else { //this will run if all the above is false }– Ex:
• if (today == “Friday”) { goToMosque(); } else if (today == “Saturday”) { readABook(); } else { startCoding(); }In human terms: ‘If today is Friday then go to the Mosque, if it’s Saturday then read a book otherwise start writing codes’
Comparison Operators• == Equality operator checks
whether the value on the left is equal or equivalent to the value on the right
• > Greater than and < less than operator checks whether a value is greater than or less than another number
– Like: if (examMark > 50) {//you have passed}
• >= Greater than equal to and <= less than equal to
• Functions that check whether a value is boolean can be used in if statements• if(isNaN(today)) { checkTheDay(); }
• Check for a null object by– if (today) { checkTheDay(); }– if (today != null) { checkTheDay(); }
• ‘if today is not null then check the day of the week’
• && And and || or operators are used to join together multiple conditions
– if(today == “Monday” || today == “Sunday”) { startCoding(); }
– if(today == “Saturday” && today == “Sunny”) { startCycling(); }
• != Not equal to will produce the opposite of the expression
– An equality can be negated directly: if(!(today == “Friday”)) {readABook();}
• ‘if today is not Friday then read a book’
– if(today != “Friday”) {readABook();}• ‘if today is not Friday then read a book’
Remember:= is assignment operator== is equality operator
Remember:null or undefined will always return false in if statements
Conditional operator• This operator takes three operands and works similarly like the if
statement• Format: (logical expression) ? if true : if false;
– (today == “Sunday”) ? startCoding() : readABook();• ‘if today is Sunday then start writing codes but if today is not Sunday then read a book’
Same as:if(today == “Sunday”) { startCoding();} else { readABook();}
• The entire statement can be assigned to a variable at once– whatToDo = (today == “Sunday”) ? startCoding() : readABook();– today = today ? today : “Saturday”;
• ‘if the variable today is not null then set the default value otherwise set the string “Saturday ” to it’
Switch / Case / Default• Giant block of if…else statement• It tests a single expression against several possible outcomes and if none is
exact then set a default value• If a match is found the chain is broken using the keyword break • switch(today) {
case “Friday”: goToMosque();break;case “Sunday”: startCoding();break;default: readABook();
}
Equivalent to writing
if (today == “Friday”) { goToMosque();} else if (today == “Sunday”) { startCoding();} else { readABook();}
Loops• Loops count up or down through a set of data• Allows us to repeat a given action multiple times• Ex: count through a list, search through an array of items or output a set
of characters like A-Z on the screen• Three distinct type of looping structures
– For loop: for loop has two sub-categorical structures• for…in loop• for…each loop
– While loop– Do..while loop
For Loops• Has similarity with C, Java, PHP
and other languages• The variable that is defined in the
loop is explicitly available for that scope only
• i stands for index – Conventional variable name but
anything name can be used
• for….in loop iterates through every value stored in an object
• for each…in iterates the properties or functions directly rather than iterating the property-names
• for (var i:int = 1; i <= 100; i++) { //put some code that will print out 100 times}‘First declare a new variable with an integer type and assign a value of 1 and repeat an action 100 times by incrementing or adding 1 to the previous value’
• for (var value:String in targetObject) { //do something}
• var weekDays:Object = new Object();• weekDays.first = “Sunday”;• weekDays.second = “Monday”;• weekDays.third = “Tuesday”;• for (var value:String in weekDays) {
trace(value +” : ”+ weekDays[value]) }
• for each (var value:String in targetObject) { //do something}
– for each (var value:String in weekDays) {trace(value);
}
While and Do…while Loops• While loop will iterate as long as
the given condition is true
• While loops are useful when the number of iteration is unknown
• Do…while loop checks the condition after one iteration is complete
• while (condition == true) { //perform some action }
• var i:int = 0; while (i <= 100) { //perform some action i++; }• do { //perform an iteration } while (condition == true);• var i:Number = 1; do { i++; } while(i <= 100); trace(i +" is greater than 100");
for (var i:int = 0; i <= 100; i++) { //perform some action}
Same as
Comments• Comments are codes that are not executed when a compiler is compiling
the code• They can be referred to as sticky notes that explains the code
that we write into further details to help our fellow programmers
• Add comments where necessary because they can be useful if we haven’t looked into a piece of code for a long time
• Types of comments:– Single line comment:
• var i:int = 0; // this is an integer-type variable– Block comment:
• /* this is a block comment where large texts can be added */
Remember:Comments are codes too!
Happy Coding&
Salam