• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Action Script
 

Action Script

on

  • 697 views

Action Script

Action Script

Statistics

Views

Total Views
697
Views on SlideShare
697
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Action Script Action Script Presentation Transcript

    • Presented By AngelinAngelin
    •  Object-Oriented Programming Language ◦ Encapsulation ◦ Abstraction ◦ Inheritance ◦ Polymorphism  ECMA compliant & procedural language  ActionScript code is defined in files with the .as extension or embedded within MXML files using one of the following methods: ◦ Embed ActionScript code in mxml files using the tag <mx:Script> ◦ Include external ActionScript files into mxml, for example : <mx:Script source=”calculateTax.as”> ◦ Import ActionScript classesAngelin
    •  ActionScript variables are strongly typed.  Syntax: var varName:VarType = new VarType();  ActionScript variable definitions: ◦ var object:Object = new Object(); ◦ var array:Array = new Array(); ◦ var count:int = 1; // short-hand int definition ◦ var name:String = "Donnie Brasco"; // short-hand string definition ◦ var names:Array = { "Alice", "Bob", "Carl" }; // short-hand array definition  ActionScript does support type-less variables, but it is generally not encouraged. ◦ var var1 = null; // un-typed variable ◦ var var2:* = null; // any-typed variable  ActionScript also supports using "*" (wildcard) as a variables. ◦ var varName:*; The "*" wildcard type means any object type can be assigned to the variable.Angelin
    •  int - Represents a 32-bit signed integer.  Number - Represents an IEEE-754 double-precision floating-point number.  Boolean - can have one of two values, either true or false, used for logical operations.  String - Represents a string of characters.  Date – Represents date and time information.  Array – To access and manipulate arrays.  XML - The XML class contains methods and properties for working with XML objects.  XMLList - The XMLList class contains methods for working with one or more XML elements.  uint – A class that provides methods for working with a data type representing a 32- bit unsigned integer.  Error - Contains information about an error that occurred in a script.  RegExp - To work with regular expressions, which are patterns that can be used to perform searches in strings and to replace text in strings.Angelin
    •  ActionScript properties can be declared by using five different access modifiers: ◦ internal: Makes the property visible to references inside the same package (the default modifier) ◦ private: Makes the property visible to references only in the same class ◦ protected: Makes the property visible to references in the same class and derived classes ◦ public: Makes the property visible to references everywhere ◦ static: Makes the property accessible via the parent class instead of instances of the classAngelin
    •  An ActionScript class must define a public constructor method, which initializes an instance of the class.  A constructor has the following characteristics: ◦ No return type. ◦ Should be declared public. ◦ Might have optional arguments. ◦ Cannot have any required arguments if you use it as an MXML tag. ◦ Calls the super() method to invoke the superclass‟s constructor. The super() method is called from within the constructor which invokes the superclass‟s constructor to initialize the items inherited from the superclass. The super() method should be the first statement in the constructor;Angelin
    •  Function Definition syntax: access_modifier function functionName(argName:ArgType, optionalArgName:optionalArgType=defaultValue):ReturnType { Statement1; .. StatementN; }  Invoking a function var returnValue:ReturnType = functionName(argName);  Examples: public function doSomething( arg:Object ):String { return "something"; } //function with an optional argument public function anotherFunction( arg:Object, opt:String=null ):void { // do something }Angelin
    •  Events Actions that occur in response to user-initiated and system-initiated actions  Event Handling The technique for specifying certain actions that should be performed in response to particular events.  Example function eventHandler(event:MouseEvent):void { // Actions performed in response to the event go here } myButton.addEventListener(MouseEvent.CLICK, eventHandler);Angelin
    •  ActionScript class definition: package packageName { import another.packageName.*; public class ClassName extends SuperClassName implements InterfaceName { public ClassName() { // constructor } } }Angelin
    •  ActionScript interface definition: package packageName { public interface InterfaceName extends AnotherInterface { // public not necessary (and not allowed) function methodName():void; } }  ActionScript Interfaces do not allow property definitions.Angelin
    •  Runtime Type Checking is done using the „is‟ operator.  Casting of objects is done using the „as‟ operator. The ActionScript „as‟ operator will cast the object and assign it to the variable, but only if object is of the proper type (otherwise it assigns null). ActionScript usage of the „as‟ operator: // use "as" to safely assign object as a Person or null var person:Person = object as Person;  ActionScript runtime type checking and casting example: // cast Object to Person if( object is Person ) { var person:Person = Person ( object ); }Angelin
    •  ActionScript "for" and "for each" loops: // “for” loop to loop over an array for( var i:int = 0; i < array.length; i++ ) { // use array[i] } // "for each" loop to loop over a collection var item:Object; for each( item in collection ) { // use item }Angelin
    •  ActionScript throws exceptions and supports the try/catch/finally structure for handling them.  ActionScript exception handling using try/catch/finally: // functions do not declare that they throw exception public function doSomething():void { try { // try something } catch( error:Error ) { // handle error by rethrowing throw error; } finally { // executed whether or not there was an exception } }Angelin
    •  Initial comment  Package definition  Imports  Metadata ◦ Event ◦ Style ◦ Effect  Class definition ◦ Static variables ◦ Instance variables  public constants  Internal  protected  private constants ◦ Constructor ◦ Getter/setter methods for variables ◦ MethodsAngelin
    • Java class definition ActionScript class definition package packageName; package packageName { import another.packageName.*; import another.packageName.*; public class ClassName extends public class ClassName extends SuperClassName implements SuperClassName implements InterfaceName InterfaceName { { public ClassName() public ClassName() { { // constructor // constructor } } } } }Angelin
    • Java interface definition ActionScript interface definition package packageName; package packageName { public interface InterfaceName extends public interface InterfaceName extends AnotherInterface AnotherInterface { { // public is optional (and // public not necessary (and not extraneous) allowed) public void methodName(); function methodName():void; } } } Unlike Java, ActionScript Interfaces do not allow property definitions.Angelin
    • Java variable definitions ActionScript variable definitions Object object = new Object(); var object:Object = new Object(); int count = 1; var count:int = 1; String name = “Don Bradman"; var name:String = "Don Bradman"; String[] names = { "Alice", "Bob", var names:Array = { "Alice", "Bob", "Carl" }; "Carl" }; // ActionScript arrays are untyped ActionScript does support type-less variables, but it is generally not encouraged. var var1 = null; // un-typed variable var var2:* = null; // any-typed variableAngelin
    • Java method definition ActionScript method definition public String doSomething( Object public function doSomething( arg:Object arg ) ):String { { return "something"; return "something"; } } ActionScript also allows optional function arguments. ActionScript function definition with optional function argument: public function anotherFunction( arg:Object, opt:String=null ):void { // do something }Angelin
    • Java runtime type checking and ActionScript runtime type checking and casting casting // cast Object to Person // cast Object to Person if( object instanceof Person ) if( object is Person ) { { Person person = (Person) var person:Person = Person( object ); object; } } For runtime type checking ActionScript uses the „is‟ operator. For runtime type checking Java uses the „instanceof ‟operator ActionScript also offers another method, similar to casting, using the „as‟ operator. The ActionScript „as‟ operator will cast the object and assign it the variable, but only if object is of the proper type (otherwise it assigns null). // use "as" to safely assign object as a Person or null var person:Person = object as Person;Angelin
    • Java "for" and "for each" loops ActionScript "for" and "for each" loops // loop over an array // loop over an array for( int i = 0; i < array.length; i++) for( var i:int = 0; i < array.length; i++) { { // use array[i] // use array[i] } } // loop over a collection // loop over a collection for( Object item : myCollection) for each(var item:Object in myCollection) { { // use item // use item } }Angelin
    • Java exception handling ActionScript exception handling // method declared to throw an // functions do not declare that they exception throw exception public void doSomething() throws public function doSomething():void Exception { { try try { { // try something // try something } } catch( error:Error ) catch( Exception ex ) { { // handle error by rethrowing // handle exception by throw error; rethrowing } throw ex; finally } { finally // reached whether or not there is { an exception // reached whether or not there } is an exception }Angelin } }
    • Java Accessor Functions (Get/Set) ActionScript Accessor Functions (Get/Set) public class Person public class Person { { private String name = null; private var _name:String; public String getName() public function get name():String { { return this.name; return _name; } } public void setName( String name ) public function set name( value:String { ):void this.name = name; { } _name = value; } } }Angelin
    • Java Singleton Class ActionScript Singleton Class package examples; package examples { public class Singleton public class Singleton { { private static final Singleton _instance static private const _instance:Singleton = new = new Singleton(); Singleton(); private Singleton() // private constructors not supported {} public Singleton() { public Singleton getInstance() if( _instance ) { { return _instance; throw new Error( "Singleton instance already } exists." ); } } } static public function get instance():Singleton { return _instance; } }Angelin }
    • Concept/Language Java 5.0 ActionScript 3.0 Construct Class library packaging .jar .swc class Employee extends class Employee extends Inheritance Person{…} Person{…} var firstName:String=”John”; String firstName=”John”; var shipDate:Date=new Variable declaration and Date shipDate=new Date(); Date(); initialization int i;int a, b=10; var i:int;var a:int, b:int=10; double salary; var salary:Number; It‟s an equivalent to the wild card type notation *. If you Undeclared variables Not Applicable declare a variable but do not specify its type, the * type will apply. If you write one statement per Terminating statements Mandatory line you can omit semicolon with semicolons at the end of that statement.Angelin
    • Concept/Language Java 5.0 ActionScript 3.0 Construct No block scope. block: declared within curly local: declared within a braces, function local: declared within a method member: declared at the Variable scopes or a block class level member: declared at the class global: If a variable is level declared outside of any global: no global variables function or class definition, it has global scope. Immutable; stores Immutable; stores sequences of Strings sequences of two-byte two-byte Unicode characters Unicode characters for strict equality use === Strict equality operator Not Applicable for strict non-equality use !== The keyword final The keyword const Constant qualifier final int STATE=”NY”; const STATE:int =”NY”;Angelin
    • Concept/Language Java 5.0 ActionScript 3.0 Construct The top class in the Object Object inheritance tree Dynamic (checked at run-time) and static Static (checked at Type checking (it‟s so called „strict mode‟, which is default compile time) in Flex Builder) is – checks data type. Type check i.e. if (myVar is String){…} instanceof operator The „is‟ operator is a replacement of older instanceof Similar to „is‟ operator, but does not return Boolean, instead returns the result of expression The „as‟ operator Not Applicable var orderId:String=”123″; var orderIdN:Number=orderId as Number; trace(orderIdN);//prints 123 Person p=(Person) var p:Person= Person(myObject); CastingAngelin myObject; orvar p:Person= myObject as Person;
    • Concept/Language Java 5.0 ActionScript 3.0 Construct All primitives in ActionScript are objects. byte, int, long, Boolean, int, uint, Number, String. float, Primitives The following lines are equivalent: double,short, var age:int = 25; boolean, char var age:int = new int(25); Array, Date, Error, Function, RegExp, XML, Complex types Not Applicable and XMLList var quarterResults:Array int =new Array(); quarterResults[]; orvar quarterResults:Array=[]; quarterResults = Array declaration var quarterResults:Array= new int[4]; and instantiation [25, 33, 56, 84]; int AS3 also has associative arrays that uses quarterResults[]={ named elements instead of numeric indexes 25,33,56,84}; (similar to Hashtable). var myObject:*; Un-typed variable Not Applicable var myObject;Angelin
    • Concept /Language Java 5.0 ActionScript 3.0 Construct package com.xyz{class myClass{…}} package com.xyz; packages ActionScript packages can include not only class myClass {…} classes, but separate functions as well public, private, public, private, protected protected if none is specified, classes have internal Class access if none is specified, access level (similar to package access level in levels classes have package Java) access level Similar to XML namespaces. Custom access namespace abc; levels: Not Applicable abc function myCalc(){} namespaces or abc::myCalc(){}use namespace abc ;Angelin
    • Concept /Language Java 5.0 ActionScript 3.0 Construct Hashtable, Associative Arrays allows referencing its MapHashtable friends = elements by names instead of indexes. new Hashtable(); var friends:Array=new Array(); friends.put(“good”, friends["good"]=”Mary”; “Mary”); friends["best"]=”Bill”; friends.put(“best”, friends["bad"]=”Masha”; Unordered “Bill”); var bestFriend:String= friends["best"]; key-value friends.put(“bad”, friends.best=”Alex”; pairs “Masha”); Another syntax: String bestFriend= var car:Object = {make:”Toyota”, friends.get(“best”);// model:”Camry”}; bestFriend is Bill trace (car["make"], car.model); // Output: Toyota CamryAngelin
    • Concept /Language Java 5.0 ActionScript 3.0 Construct Console // in debug mode only System.out.println(); output trace(); import com.abc.*; import com.abc.*; import com.abc.MyClass; imports import packages must be imported even if the class com.abc.MyClass; names are fully qualified in the code. Compiler moves all variable declarations to the top of the function. So, you can use a Hoisting Not Applicable variable name even before it has been explicitly declared in the code.Angelin
    • Concept/Language Java 5.0 ActionScript 3.0 Construct Customer cmr = var cmr:Customer = new Customer(); new Customer(); var cls:Class = Instantiation Class cls = flash.util.getClassByName(“Customer”); objects from Class.forName(“C var myObj:Object = new cls(); classes ustomer”);Object myObj= cls.newInstance(); private class There is no private classes in AS3. Private classes myClass{…} Supported. Not available. Typical use: Implementation of private constructors is singleton classes. postponed as they are not the part of the ECMAScript standard yet. Private constructors To create a Singleton, use public static getInstance(), which sets a private flag instanceExists after the first instantiation.Angelin Check this flag in the public constructor, and if instanceExists==true, throw an error.
    • Concept/Language Java 5.0 ActionScript 3.0 Construct A file can have multiple A file can have multiple class class declarations, but declarations, but only one of them Class and file only one of them can be can be placed inside the package names public, and the file must declaration, and the file must have have the same name as the same name as this class. this class. dynamic class Person {var name:String;} Dynamic classes //Dynamically add a variable and a function (define an object Person p= new Person(); that can be altered p.name=”Joe”; at runtime by Not Applicable p.age=25; adding or changing p.printMe = function () { properties and trace (p.name, p.age); methods). }Angelin p.printMe(); // Joe 25
    • Concept/Language Java 5.0 ActionScript 3.0 Construct Not Applicable. myButton.addEventListener(“click”, Closure is a proposed myMethod); addition to Java 8. A closure is an object that represents a snapshot of a function with its lexical Function closures context (variable‟s values, objects in the scope). A function closure can be passed as an argument and executed without being a part of any object Abstract classes Supported Not Applicable class A implements B class A implements B {…} {…} Interfaces can contain only function Interfaces Interfaces can contain declarations. method declarations and final variables. Classes and interfaces Classes, interfaces, variables, functions, What can be placed namespaces, and executable in a packageAngelin statements.
    • Concept/Language Java 5.0 ActionScript 3.0 Construct Supported. You must use the override Function overriding Supported qualifier Function Supported Not supported. overloading Keywords: try, catch, throw, finally A method does not have to declare Keywords: try, catch, exceptions. throw, finally, throws. Can throw not only Error objects, but Exception handling Uncaught exceptions also numbers. For example, are propagated to the throw 25.3; calling method. Flash Player terminates the script in case of any uncaught exception. Regular Supported Supported expressionsAngelin
    •  Almost anything that you can do in a custom ActionScript custom component, you can also do in a custom MXML component. However, for simple components, such as components that modify the behaviour of an existing component or add a basic feature to an existing component, it is simpler and faster to create them in MXML.  When your new component is a composite component that contains other components, and you can express the positions and sizes of those other components using one of the Flex layout containers, you should use MXML to define your component.Angelin
    •  To modify the behaviour of the component, such as the way a container lays out its children, use ActionScript.  To create a visual component by creating a subclass from UIComponent, use ActionScript.  To create a non-visual component, such as a formatter, validator, or effect, use ActionScript.  To add logging support to your control, use ActionScript.Angelin
    • Angelin