JaiswalTraining

Get the online training        


Get Best Tutorial on all major programing language


Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Action Script Basics

Overview
  • The ActionScript programming language is used in Adobe’s Flash Player
  • It is included built-in objects and functions that allow you to create your own objects
    and functions like many object-orientated programming (OOP) languages.
  • AS3 is the the first version to have full support for Object oriented programming
Comments
  • A comment is a sequence of delimitedtext that is ignored by the Flex compiler.
  • Flex’s ActionScript language supports two popular formats:
    • Inline comments with regular code,
    • Block style with multiline comments.

Inline comments
  • The first comment type is the inline style which you invoke
    using double-forward slashes:
// one comment
var i:int;
var x:int// another comment

Block Comments
  • If you want to provide a much larger description using free-form text, you can use a
    multiline comment instead. 
  • Begin a multiline comment by typing a forward slash and
    asterisk (/*), and close it with the inversion (*/):

/*
var i:int;
var x:int; 

*/


Variables

  • Variables are containers used to store values.
  • Variable names should help explain the significance of the value being store
  • Variable names may contain only letters, numbers, underscores, and the dollar sign.
  • Unlike JavaScript, ActionScript uses strict data typing.
  • Syntax :----    var var_name:var_type
  • As the following code shows:.
var age:Number;
age = "thirty"//would result in an error

Data type
  • Primitive Data Type
  • Complex Data type

Primitive Data Type

Boolean It is used to store true or false
int It is used to store whole number.Its size is 32 bit
uint It is similar to int but it can only store non-nagetive numbers.
Number It is more generic form ofnumeric data type. it can store deciumal as well. its size is 64 bits
String It is a sequence of characters.It is used to store text.

Complex Data Type

Obejct It is used to store true or false
Array It is an object of similar data accessed via numeric index.
Date It provides a number of functions for date manipulation
Error It is used for handling exceptions and managing unexpected conditions in your application.


Loops
  • Loop is the iterative statement.
  • It iterates using the some counter variable till the condition is true.
  • The counter variable may be numeric, boolean or charcter or the String variable. 
  • Types
    • for
    • while
    • do while
For Loop
  • This is the easiest loop among all the loops
  • It has declaration, condition checking, decrement  increment  all at one place  
  • It is the pre tested loop
  • This type of loop comes in handy when you know how many times
    you need to perform a task
Syntax

for(counter intialization;condition;decrement/increment)
{
statement
}

for(var x:int=1;x<=10;x++)
{
//some code
}

While Loop
  • It is called pre tested loop
  • In this condition is checked at the satating of the loop
  • Statement is given in body part
  • In the body part counter variable is increnmented or decremented
Syntax

counter variable
               while(condition)
                {
                    statement
                    
              conter_var incr,decr
                 
                }


Do while Loop
  • It is called post tested loop
  • In this condition is checked at the end of the of the loop
  • So it executes at once in case of false condition
  • In body part the statement is given 
  • In the end while is ended by the semicolon
Syntax

counter variable
             do
                {
                    statement
                    conter_var incr,decr
                }
             while(condition);


Conditional statements
  • ActionScript implements two forms of conditional statemnents
    • if
    • switch 
if..else statement
  • It can be used to route program execution through two different paths based on condition.
syntax
if (condition)
{
statement
}
else
{
statement
}

Example: Checking password statically.

if_else.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
  <mx:Script>
    <![CDATA[
      public function checkpwd(txt:String):void
      {
        if(txt=="javed")
          mx.controls.Alert.show("valid Password");
        else
          mx.controls.Alert.show("Invalid Password");    
      }
    ]]>
  </mx:Script>
  <mx:TextInput id="pwd"/>
  <mx:Button label="Validate" click="checkpwd(pwd.text)"/>
</mx:Application>

Output:
  • If the password is javed then It is valid.




  • If the password other than javed than it is invalid
.


Download code

switch statement
  • It provides an easy way to dispatch execution to different parts of your code
    based on the value of an expression.
syntax
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
case valueN:
// statement sequence
break;
default:
// default statement sequence
}

Example:- Getting Remarks on the base of grade.

swtichtest.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
  <mx:Script>
    <![CDATA[
      public function grade():void
      {
        switch(gd.text)
        {
          case 'A'
            mx.controls.Alert.show("Excellent...!!!");
            break;
          case 'B'
            mx.controls.Alert.show("Good...!!!!!");
            break;
          case 'C'
            mx.controls.Alert.show("Not Bad...!!!!!");
            break;
          default:
            mx.controls.Alert.show("Failed...!!!!! ");
        }            
      }
    ]]>
  </mx:Script>
  <mx:TextInput id="gd"/>
  <mx:Button label="Check Remark" click="grade()"/>
</mx:Application>

Output:-

   



 


Download Code

Arrays
  • An array is a variable that can store multiple value.
  • ActionScript supports three main types of arrays:
    • Indexed
      • Use a number as a key for each value.
    • Associative
      • Use any kind of a key (usually a  string) to associate values to the key
    • Multidimensional
      • Arrays of arrays

  • There are several ways you can create an Array object.
  • The simplest is to use the Array constructor using the new operator.
    var myArray:Array = new Array();

    // an array with five "slots" ready to hold data
    var myArray:Array = new Array(5);
Manipulating Array
  • push()—Appends the item onto the end of the array.
  • pop()—Takes an item off the end of the array.
  • shift()—Takes an item off the front of the array.
  • unshift()—Inserts the item onto the front of the array.
  • splice()—Remove items from a position, and optionally insert new items.
    The first line adds an item in the second position.  
    The next line takes out the second item.

Example:--
array1.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  <mx:Script>
    <![CDATA[
    function  show():void{
      var ar:Array=new Array(3);
      ar[0]="ram";
      ar[1]="mohan";
      ar[2]="sohan";      
      list1.dataProvider=ar;
      }
    ]]>
  </mx:Script>
  <mx:List x="68" y="51" width="131" id="list1"  click="show()"></mx:List>
</mx:Application>

Output:-
  • When user click on the list then it will display array elements 
  • As shown below.
   
Download code

Example:-2 Adding elements in array at runtime

array3.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 
layout="absolute" initialize="show()">
  <mx:Script>
  </mx:Script>
  <mx:List x="68" y="10" width="131" id="list1"  height="222"/>
  <mx:TextInput x="207" y="35" width="115" id="t1"/>
  <mx:Button x="226" y="90" label="Add" click="add()"/>
  <mx:Script>
    <![CDATA[
    var ar:Array=["ram","mohan","sohan"];      
      
    function  show():void{
      //var ar:Array=["ram","mohan","sohan"];      
      list1.dataProvider=ar;
      }
      function add():void{
      ar.push(t1.text);    
      list1.dataProvider=ar;      
      }
      
    ]]>
  </mx:Script>  
  </mx:Application>

Output:-

   

Download code

Example:-3 Removing elements in array at runtime

array4.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" initialize="show()">
  <mx:Script>
  </mx:Script>
  <mx:List x="68" y="10" width="131" id="list1"  height="222"/>
  <mx:Button x="226" y="90" label="Remove" click="remove()"/>
  <mx:Script>
    <![CDATA[
    var ar:Array=["ram","mohan","sohan","rohan","sita","gita","rita"];      
      
    function  show():void{
      list1.dataProvider=ar;
      }
      function remove():void{
      ar.pop();    
      list1.dataProvider=ar;      
      }
      
    ]]>
  </mx:Script>  
  </mx:Application>

Output:-
  • pop()—Takes an item off the end of the array.
  • As shown below. 
  • when user click on remove button,last element of array will get deleted.



Download code

Example:- Splicing an array element.

array7.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 
layout="absolute" initialize="show()">
  <mx:Script>
  </mx:Script>
  <mx:List x="21" y="10" width="131" id="list1"  height="222"/>
  <mx:TextInput x="264" y="65" width="55" id="t2"/>
  <mx:TextInput x="264" y="35" width="85" id="t0"/>
  <mx:Button x="234" y="120" label="splice" click="splice()"/>
  <mx:Script>
    <![CDATA[
    var ar:Array=["ram","mohan","sohan"];      
      
    function  show():void{
      //var ar:Array=["ram","mohan","sohan"];      
      list1.dataProvider=ar;
      }
      function splice():void{
         var p:Number=parseInt(t2.text);
      ar.splice(p,1,t0.text);    
      list1.dataProvider=ar;      
      }
      
    ]]>
  </mx:Script>  
  <mx:Label x="207" y="67" text="position"/>
  <mx:Label x="207" y="37" text="text"/>
  </mx:Application>

Output:-
  • We are replacing sohan with javed.with the user of splice() method.
  • as shown below



Download code



Functions
A function is a set of instructions which is needed to be executed for a specified task.

Syntax

scope function functionName(paramerter1,parameter2,......):returnType {
statements;
}

  • A function name must Consist only of
    •  letters, digits, the underscore character, and the dollar symbol
    • Not start with a number
    • Not be a reserved word
    • Be unique
Creating own function(hello)

myfirstfunction.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
layout="horizontal">
  <mx:Script>
    <![CDATA[
      public function hello(msg:String):void
      {
        mx.controls.Alert.show(msg);
      }
    ] ] >
  </mx:Script>
  <mx:TextInput id="clickme" text="”click"/>
  <mx:Button label="show" click="hello(clickme.text)"/>

CDATA
  • Flex MXML files are XML-compliant..
    CDATA instructs Flex Builder (and its compiler) to take the content within the square
    brackets ([]) as is and not try to process it as XML code.
Run it


  • User-entered text is passed to a function



Download code

Separating ActionScript to individual files
  •  ActionScript doesn’t need to reside in your MXML files.
    you can split it into its own files. 
  • This makes it easier to maintain your code as your applications grow in size,
    as well as making your functions more reusable by allowing multiple applications to
    source-in their logic.
  • To do this, pull out any ActionScript into a file, and save it with a .as extension as
    shown below

helloworld.as
public function hello(input1:String,input2:String):String
{
  var x:String = input1 + input2;
  return x;
}
Download code

main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
  <mx:Script source="helloworld.as"/>
  <mx:Script>
    <![CDATA[
      import mx.controls.Alert;
    ]]>
  </mx:Script>
  <mx:TextInput id="value1"/>
  <mx:Label text="and"/>
  <mx:TextInput id="value2"/>
  <mx:Button label="Click here" click="Alert.show(hello(value1.text,value2.text))"/>
</mx:Application>

Output:-




Download Code