adobe flash actionscript language basics chapter-2

Download Adobe Flash Actionscript language basics chapter-2

Post on 30-Jun-2015




1 download

Embed Size (px)


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:


  • 1. NafissClassroomahmdnafis@gmail.comActionScript PrimerChapter - 2

2. Overview : What is ActionScript An object-oriented programming language used to build Flashcontent 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 drawprogrammatically Has similarities with Ajax or JavaScript because it relies onDOM (Document Object Model) and responds to clicks ormouse events, for example. 3. Variables, constants, scopes, operators, conditional statements, loopsand commentsTIME FOR SOME BASICS! 4. Variables / Storage System They are temporary storagesystems that hold string ofcharacters, numbers orother values They contain informationthat are called objects whenthe program is running They are also known asproperties when they are apart of a class Usage of underscores,numbers and letters areallowed To declare a variable andassigning 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 usedwhen naming variables ex: myName = whatever 5. Constants A special kind of variablethat never changes whenthe program is running Produces an error when anattempt is made to changethe constants value Constants name should beall uppercase letters(recommended) Can be declared at thebeginning of a class or inthe constructor for the class To declare a constant: const DAYS:int = 7; 6. Be Literal Buddy! (Literals) Literal means the value that isexplicitly included in the codewhen its compiled Beware: use of literals is notrecommended cause they can behard to track when programneeds to be debugged Regular expressions with forwardslashes like /href=#/ XML elements like Generic objects like {city: Dhaka, name: Nafis} Array literals like [a, b, c] Empty values like void, undefined or null Strings with double or singlequotes Like literals Boolean values Like true and false Numbers of type int or uint like 100, 3.22 7. Access Controls Written before aproperty/variable or amethod/class-function Four types of access controlattributes: 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 theproperty or the method it markedwith this keyword Private: Property or methodnamed private can only beaccessed within a class Internal: Neither public norprivate but internal objects areaccessible to classes within thesame class package/ any classthat has the same packagedeclaration This is the default access control forall objects in AS 3.0 Protected: Protected objects canbe accessed by child orsubclasses/ when a class isextended 8. 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 beregarded as in the global scope and can be accessed from anywhere Local scope: Objects inside any class or function can be accessed onlyin their given scope/space not from everywhere. Local scope hasnumerous layers 9. Layers of Local Scope Class level/Static variables andmethods exist in the class itself,not requiring an instantiation ofthe class Instance level variables &functions exists in every singleinstance of a class Objects defined in functions thatare housed inside classes are onlyavailable temporarily Variables or properties inside theclass is available to methods orfunctions Functions parameters exists onlyinside the scope of the function Objects in more localized scopes willoverride objects in more globalscopes 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(; // produces: Nafis To access an instance variable: public class scope2 {public var postalCode:Number = 1200;} var newS2:scope2 = new scope2(); trace(newS2.postalCode); // outputs1200 10. Scope inside a function: public class scope3 {public function hello():void {var msg:String = Whats Up!;trace(msg);}} var newS3:scope3 = scope3(); newS3.hello(); // outputs: Whats Up! trace(newS3.hello); // undefined 11. Explaining Data Types A data type refers to the data thatcan be stored in the object forexample Every object/variable orfunctions parameter or returntype need to have a data type Objects with unknown type canhave a wildcard(*) data type Usage of colon operator iscompulsory when declaring adata 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; 12. 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 (*, /, %) andare evaluated from left to right Addition and subtraction have equal importance (+, -) and are evaluated from left toright 13. Lets Meet the Operators! The commonly used arithmeticoperators + Addition - Subtraction * multiplication / division % modulo operator returns theleftover or the remainder afterdividing two numbers like 4 % 3 = 1 Compound assignment operatorsperform arithmetic operation onnumbers and reassign the answerto the number, like: x += 1, whichis also x = x + 1 Compound assignment operatorsare += Add and reassign *= Multiply and reassign -= Subtract and reassign /= Divide and reassign %= Get the remainder from divisionand reassign it to the variable 14. Conditionals Conditionals help to produce different outcomes for varied circumstancesby evaluating the truth of logical expression Every conditional statement relies on Boolean operation. Booleans areobjects 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 15. If / else if / else If statements only evaluates when acertain/given condition is true Additional if statements can beadded with else if if the firstcondition in the first statement isnot true Lastly, an else statement can beadded as a default block if all theother if statements are evaluatedto 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 isfalse} 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 goto the Mosque, if its Saturday then read abook otherwise start writing codes 16. Comparison Operators == Equality operator checkswhether the value on the left isequal or equivalent to the valueon the right > Greater than and < less thanoperator checks whether a valueis greater than or less thananother number Like: if (examMark > 50) {//you havepassed} >= Greater than equal to and

View more