Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 2 of 2

Thread: Tutorial 5 - The conditional logics

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Tutorial 5 - The conditional logics

    flash templates

    Tutorial 5 – conditional logics


    I carry on with the series of tutorials of basic Actionscript 3.0.

    This time, I would like to introduce you to the conditional logics.

    Sometimes we need to make a choice on which type of code to be carried out by Flash based on a certain condition.

    Actionscript 3.0 allows to make those choices using conditional logics that if are revealed to be right, performs a block of code or another.

    This way, we can direct Flash to carry out a chosen sequence of code based on the different present events.


    The most used operators of conditional logics are the following:

    - if

    - else

    - else if

    - switch


    Let us see when and how to use them…


    if


    Let us say the we have a String type variable.


    Code:
    var my_name:String='filippo';

    and that we want Flash to perform a certain code based on its value.

    In this case, we need Flash to check first the value of ‘my_name’ and next, perform the correspondent code.

    So:



    Code:
    if(my_name=='filippo')
    {
    	trace('ok');
    }

    if the word says it all. We are telling Flash to check out something. What? This is what is coming next in between the parenthesis. In fact…

    (my_name=='filippo') tells Flash that the value of ‘my_name’ is equal to ‘filippo’ (in Actionscript, the operator of equability in a ‘if’ is the symbol ==)

    So, I obtain the following output:

    ok

    If the value of ‘my_name’ would not be ‘filippo’, from the example seen above Flash would not perform any code.

    Instead, if we wanted Flash to perform another code in case the value is not ‘filippo’?

    Simple. We would use…


    else


    the following way:


    Code:
    var my_name:String='luca';
    
    if(my_name=='filippo')
    {
    	trace('ok');
    }
    else
    {
    	trace('the value of my_name is not filippo');
    }

    else, as the word says it, Flash performs the code in between the brackets if the requested condition placed in the ‘if’ is not true.

    This time, I obtain the following output:

    the value of my_name is not Filippo

    Flash first check out the ‘if’ condition if it is true or not. As it is false, Flash ignores the codes placed in the brackets of the ‘if’ and carries out the code placed in the brackets of the ‘else’.


    Let us look at another example with a double condition and the logic &&.

    I have two variables, a String and a Boolean.

    I have a function that change the value of the string variable and I apply the conditional logic if/else:


    Code:
    var your_name:String='philip';
    var male:Boolean=true;
    
    function changeName():void
    {
    	your_name='johnny';
    }
    
    if(your_name=='philip'&&male==true)
    {
    	trace('riight condition');
    }
    else
    {
    	trace('wrong condition');
    }

    Clearly, if I do not call the function, Flash performs the ‘if’ as the condition is true.

    NB: the operator && tells Flash that there is a second condition. It would be the same as ‘and’.

    So, I obtain the following output

    right condition


    Instead, if I call the function changeName


    Code:
    var your_name:String='philip';
    var male:Boolean=true;
    
    changeName();
    
    function changeName():void
    {
    	your_name='johnny';
    }
    
    if(your_name=='philip'&&male==true)
    {
    	trace('right condition');
    }
    else
    {
    	trace('wrong condition');
    }

    I obtain this output

    wrong condition

    Flash has verified that the first condition in between the parenthesis of the ‘if’ is false and so performs the code placed in the ‘else’.


    Another example with the operator or logic ||

    Still using 2 variables, a String and a Boolean


    Code:
    var fruit:String='apple';
    var black:Boolean=false;
    
    if(fruit=='banana'||black==false)
    {
    	trace('at least one of both conditions is right');
    }
    else
    {
    	trace('both are wrong');
    }

    in this case, ‘if’ is as follow: of ‘fruit’ is equal to ‘banana’ or ‘black’ is equal to ‘false’, the code is carried out. It simply needs that one of the two conditions is true.

    In fact, I obtain the following output

    at least one of both conditions is right


    Instead, if for example I would change the value of ‘black’ from ‘false’ to ‘true’, none of the conditions would be exact and I would obtain the following output:

    both are wrong


    else if


    This command explains itself by its name: else if…


    Let us make an example. We are inside a cycle and we check out the value of the control variable (in this case ‘i’)


    Code:
    var max:int=20;
    for(var i:int=0;i < max;i++)
    {
    	if(i==0)
    	{
    		trace('the i has value '+i);
    	}
    	else if(i==3)
    	{
    		trace('the i has value '+i);
    	}
    	else
    	{
    		trace('the i has not value 0 and has not value  3');
    	}
    }

    I use ‘if’ to check out if the value of ‘i’ is equal to 0, ‘else if’ to check out if the value is equal to 3, ‘else’ to check out if ‘i’ is neither 0 or 3.

    Basically, if ‘i’ is equal to 0, it performs one thing, if ‘i’ is equal to 3, it performs another thing and if the value of ‘i’ is not equal to 0 or 3, it performs something else.

    I obtain the following output:

    the i has value 0

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3

    the i has not value 0 and has not value 3


    switch


    This instruction, similar to ‘if’ seen first, creates a branched structure and check out the conditions performing only the ones resulting true.


    Code:
    var new_name:String='filippo';
    switch(new_name)
    {
    	case 'luca':
    	trace(' the variable new_name has not value luca');
    	break;
    	
    	case 'filippo':
    	trace('the variable new_name has value filippo');
    	break;
    	
    	case 'giacomo':
    	trace('the variable new_name has not value giacomo');
    	break;
    }

    I create a String type variable with a value equal to ‘filippo’.

    switch(new_name) is the same as saying checkOut(new_name)

    case, if the case is 'luca'

    trace....

    break tells to Flash to ignore the rest of the code if the result is true

    In fact I obtain the following output:

    the variable new_name has value filippo

    the rest of the code is not being performed.


    I will soon publish an article about those conditional logics using them to build and control a menu and the time line.


    Stay tuned !


  2. #2
    Junior Member Settled In Ibrahim Mekky is on a distinguished road
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Re: Tutorial 5 - The conditional logics

    This tutorial and 1 - 4 before are all beautiful and very simple to understand; Please provide a zipped fiel for tutorial 5 as the 4 before. Thanks

+ Reply to Thread

Similar Threads

  1. tutorial 2 - le MovieClip
    By Flep in forum Flash CS3 base - tutorials
    Replies: 17
    Last Post: 10-07-10, 06:35
  2. Tutorial 3 - the loops
    By Flep in forum Actionscript for beginners - tutorials
    Replies: 1
    Last Post: 17-06-09, 21:49
  3. Tutorial 3 - i cicli
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 12
    Last Post: 23-06-08, 22:58
  4. Tutorial 4 - the functions
    By Flep in forum Actionscript for beginners - tutorials
    Replies: 1
    Last Post: 21-05-08, 09:31
  5. Tutorial Cercasi
    By joyz in forum Actionscript 3.0 base
    Replies: 22
    Last Post: 30-07-07, 09:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts