A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: anyone know what this is and how it works?

  1. #1
    Member
    Join Date
    Apr 2000
    Posts
    59

    SWITCH?

    <quote>
    "ActionScript Switch Statement
    Enable more straightforward ActionScript flow control statements by using the switch statement rather than a series of "if", "then", and "else" statements. This feature further increases ActionScript support for ECMA-262 as well as projects targeting Macromedia Flash Player 5."
    </quote>

    anyone know what this is and how it works? An example would be nice if you have one.thanks

    I can't afford MX right now , I'm jealous of you all!!!!

    cheers,

    nigel
    [Edited by nigel on 03-17-2002 at 07:23 PM]

  2. #2
    Senior Member
    Join Date
    Sep 2000
    Posts
    273
    Sure thing, it's a standard switch statement:
    Code:
    myVar = Math.ceil(Math.random()*5);
    
    switch(myVar)
    {
    	case 1:
    		trace ("case 1 tested true");
    		break;
    	case 2:
    		trace ("case 2 tested true");
    		break;
    	case 3:
    		trace ("case 3 tested true");
    		break;
            case 4:
                    trace ("case 4 tested true");
                    break;
            case 5:
                    trace ("case 5 tested true");
                    break;
    	default:
    		trace ("no case tested true")
    }
    Spyder

  3. #3
    Senior Front End Developper
    Join Date
    Dec 2000
    Location
    Montréal
    Posts
    568
    a switch can replace a serie of if-else if

    let's say you have this code in Flash 5:
    Code:
    //randomly select a number between 1 and 5
    var number = Math.ceil(Math.random() * 5);
    
    if (number == 1)
         trace("Apple");
    else if (number == 2)
         trace("Orange");
    else if (number == 3)
         trace("Pineapple");
    else if (number == 4 || number == 5)
         trace("Banana");
    else
         trace("Undefined");

    you could translate this in a Switch statement like this:

    Code:
    //randomly select a number between 1 and 5
    var number = Math.ceil(Math.random() * 5);
    
    switch (number) {
       case 1:
            trace("Apple");
            break;
       case 2:
            trace("Orange");`
            break;
       case 3:
            trace("Pineapple");
            break;
       case 4: 
       case 5:
            trace("Banana");
            break;
       default:
            trace("Undefined");
    }
    ps: you have to test, but I don't think this is going to work properly if the switch is not a number

    Martin

  4. #4
    Member
    Join Date
    Apr 2000
    Posts
    59
    okay,

    thanks NETSPYDER101 and CHARLESFK.


    just one other stupid question just so I'm clear on this,

    So in the great example above......When using the "switch" statement, you use case 1, case 2, case 3 etc....instead of "if(number == 1)"?

    And we are unclear if you HAVE to use numbers, right?

    I can't test the code because I can't afford MX at the moment,
    I can't even download the trail because it doesn't run on Mac OS 8.6. Have to wait for my new Mac

    thanks again

    cheers,

    nigel


  5. #5
    Senior Front End Developper
    Join Date
    Dec 2000
    Location
    Montréal
    Posts
    568
    I recommand that you DO NOT use switch...

    If-Else If will do the job just fine and are more flexible.

    I think that the player will translate your switch into iF-ElseIf anyway...


    I think it will only evaluate number or single character because it must use binary equivalent to compare element, so complex expression like string cannot be compare

  6. #6
    Member
    Join Date
    Apr 2000
    Posts
    59
    ah -- thank you charlesFK

    cheers,

    nigel

  7. #7
    Senior Member
    Join Date
    Sep 2000
    Posts
    273
    if you have more than 3 options, you should ALWAYS use switch. It is less intensive on the processor as well as easy to read in case you distribute your .fla file.

    Standard programming rules apply to AS just as much as any other programming language.

    Spyder

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center