flash learn

Upload: arijit-mitra

Post on 05-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Flash Learn

    1/7

    Chapter 5BasicsIn every programming language, there can be times when one needs to compute things or store

    some results during runtime. For this purposes, variables are used. Variables are nothing but certain

    named memory locations which are capable of storing data. These data can be modified or changed

    during the execution of program. In actionscript 3.0, these variables store chunks of information

    known as objects.

    In actionsctipt 3.0, a variable can be declared using the keyword var. the syntax is as follows:

    var : ;

    Data type represents what kind of information the variable is going to store, for example string(array

    of characters), numbers etc.

    var rAM: number; declares a variable rAM of data type number. Notice that the variable has its first

    letter in lower case. This tells us that there are restrictions to variable names. This occurs as the

    program is first converted to assembly language by the compiler then executed. So if the variables

    are named out of format, compiler may show some errors.

    The restrictions while naming the variables are:

    They should start with a letter in lower case or an underscore They can be named on a keyword(words reserved for the compilers)

    Now consider the following code.

    var clocktime:number= 10;

    trace(the time is + clocktime);

    this prints the value assigned to the clocktime that is 10 on the screen. Values can also be later

    assigned to any variable after its declaration as =. trace() prints whatever is

    inside it on the output screen.

    Another feature of actionscript 3.0 is that there is a score of making a variable constant. It means

    that this variable will now hold a constant value throughout the program and its value will not

    change. They can made using the keyword const and adding it before variable name during

    declaration.

    Apart from variables, we will also use literals in the programs. Literals are values that are explicitly

    added in the code during compile-time. Various literals which actionscript 3.0 include are:

    Numeric values Boolean values of truth and false Strings Empty values NULL and NaN Array literals Generic objects Regular expressions enclosed in forward slashes / Elements written in XML

  • 7/31/2019 Flash Learn

    2/7

    SCOPE OF A VARIABLENow it is even possible that in some parts of the program a variable cannot be used. For this it is

    necessary to know the scope of a variable. The scope decides the parts where a variable can be

    used. A variable can either be global or local. A variable which is globally defined can be used

    anywhere in the function. It can be declared at a place which is not inside any block. Local variables

    can only be used in a specific function where it is declared and vanishes as the program moves out ofa function.

    Scopes are not just restricted to variables. They are even implemented in classes, packages where

    we will revisit again in later chapters.

    DATA TYPESAnother fundamental thing regarding the variables are the data types. Data types represent what

    kind of values are going to be stored in a variable. The various types of data types are as follows:

    Int- integer values Number- floating point nos String- for characters Boolean- truth or false Void

    All the data types above are primitive data types. Various other complex data types also exist which

    will be discussed later.

    It is also possible that a variable be declared such that its type is known during run-time. For this

    Actionscript 3 supports a feature of wildcard type(*). It denotes that the data type of that variable is

    unknown till runtime or will accept values from more than two data type.

  • 7/31/2019 Flash Learn

    3/7

    CHAPTER 8Actionscript 3.0 , as we mentioned earlier, is based on OOP. It resembles more towards our day to

    day scenarios. It may so happen that we want some parts of the programs to execute based on someconditions just as in real life our decisions are based on situations. This problem is handled in AS3

    using the conditional statements.

    The various conditional statements used in AS3 are if-else, switch case and conditional operator.

    If- else:The if condition allows the programmer to get a block of executed only when a certain condition is

    met. Otherwise it moves to block which is under else just as the name suggests. 1 is considered as

    being truth whereas binary 0 represents a false condition.

    Syntax:

    If(condition) {

    }

    Else {

    }

    Note that there is no semicolon at the end of if .

    Example:

    Var rate:number =10;

    if(rate==10) {

    trace( rate is as expected as + rate);

    }

    else

    {

    trace (rate is not 10);

    }

    will print the statement rate is as expected as 10. Had the value of rate be other than 10, it would

    have printed rate is not 10.

    Just as mentioned earlier, if the condition is true, the if block is executed. So if(1) {} will always

    execute whatever is written under { }.

    Now there can also be times when we need to check multiple conditions at same time. For this

    purpose nested if-else is used. They can be of two forms:

    If(condition1){

  • 7/31/2019 Flash Learn

    4/7

    If(condition2)

    {.

    If(condition 3)

    {

    .

    }

    Else{}

    Else{}

    Else{}

    The else are executed in order from the innermost ifs else to be executed first .

    If(condition){}else{..}

    If(condition){}

    Else{..}

    ..

    SWITCH STATEMENT

    Switch statements are used for compact comparisons and executions. For example:In if-else:

    If(dayofweek==Sunday){Sunday();}

    Else If(dayofweek==Monday){Monday();}

    Else If(dayofweek==Tuesday){Tuesday();}

    Else If(dayofweek==Wednesday){Wednesday();}

    Else If(dayofweek==Thursday){Thursday();}

    Else If(dayofweek==Friday){Friday();}

    Else {saturday();}

    In switch case:

    Switch(dayofweek){

    Case Sunday: Sunday(); break;

    Case Monday: Monday(); break;

    Case Tuesday: Tuesday(); break;

    Case Wednesday: Wednesday(); break;

    Case Thursday: Thursday(); break;

    Case Friday: Friday(); break;

    Default : Saturday(); break;

    }

  • 7/31/2019 Flash Learn

    5/7

    Notice the break statements at the end of each case. If this is not done, then each case below the

    true case will get executed. This is called fall-through. To come out of switch after condition is met,

    we use break.

  • 7/31/2019 Flash Learn

    6/7

    CHAPTER 10It may so happen that we want to execute a certain block for code for a certain number of times. For

    this purpose we take use of loops. Loops are used to execute a block of code a certain number of

    times based on some conditions. There are mainly three kinds of loops that we are going to discuss:

    For Loop:It is one of the most commonly used loop in AS3. Its syntax is as follows.

    for(var :=;;)

    { //do some action}

    Here the test changes signifies the the part which will run after the block of code is executed and

    the program again returns to the for loop statement. var :=

    signifies the initialisation of the loop.

    There are various other variants of for loop as:

    1. For.in :It uses the property of an object to iterate. So for every property inside an object, it

    iterates once. For example

    For(var name:string in object)

    {//do some action}

    will iterate once for every property in object. Variable name stores the name of the

    current property in object as iteration goes on.

    2. For eachin:Only a slight different is there between these two. For each in loop retrieves all the

    values of the properties associated with the object whereas for in retrieves only the

    names. Syntax:var myObject:Object = new Object();

    myObject.myName = "AJM";

    myObject.myAge = 18;

    for each (var prop in myObject){trace(prop);}

    prints AJM and 18 whereas

    var myObject:Object = new Object();myObject.myName = "AJM";

    myObject.myAge = 18;

    for(var prop in myObject){trace(prop);}

    prints myName and myAge .

    While loop:The basic structure of a while loop is

    While(condition)

  • 7/31/2019 Flash Learn

    7/7

    {//do some action}

    It executes the block of statement as long as the condition is true.

    For example:

    Var i:int=1;

    While(i