Skip to main content
Participant
March 11, 2010
Question

Switch statement for AS3 - Flash CS3

  • March 11, 2010
  • 2 replies
  • 3038 views

Hey guys, I currently have a code of a switch statement for as1- as2.

This is it:

switch (int(Math.random() * 20))
{
    case 0:
    {
        hint = "The total amount of confusion in this world remains constant. It just gets shifted around.";
        break;
    }
    case 1:
    {
        hint = "If you experience poor performance, adjust the quality settings in your preference menu option.";
        break;
    }
    case 2:
    {
        hint = "Anticipation of death is better than death itself.";
        break;
    }
    case 3:
    {
        hint = "At the core of all well-founded belief lies belief that is unfounded.";
        break;
    }
    case 4:
    {
        hint = "The problem with instant gratification is that it takes too long.";
        break;
    }
    case 5:
    {
        hint = "Never let your schooling interfere with your education.";
        break;
    }
    case 6:
    {
        hint = "Beware of geeks bearing gifs.";
        break;
    }
    case 7:
    {
        hint = "Collaboration is essential: It allows you to blame someone else.";
        break;
    }
    case 8:
    {
        hint = "Read my MIPs - no new VAXes.";
        break;
    }
    case 9:
    {
        hint = "Where is the sea, said the fish, as they swam through it.";
        break;
    }
    case 10:
    {
        hint = "You can safely blame any bugs on Aoedipus, who is too busy grinding battlegrounds to do QA on her own work.";
        break;
    }
    case 11:
    {
        hint = "Anything worth doing is worth overdoing.";
        break;
    }
    case 12:
    {
        hint = "There is more to life than increasing its speed.";
        break;
    }
    case 13:
    {
        hint = "Don\'t worry, this will take about as long as the time it takes to play a 2v2 match with 4 ret pallies.";
        break;
    }
    case 14:
    {
        hint = "There is nothing worse than the brilliant image of a fuzzy concept.";
        break;
    }
    case 15:
    {
        hint = "If you are not part of the solution, you are part of the precipitate.";
        break;
    }
    case 16:
    {
        hint = "Two wrongs don\'t make a right, but three lefts do.";
        break;
    }
    case 17:
    {
        hint = "The heresy of today is the logic of tomorrow.";
        break;
    }
    case 18:
    {
        hint = "It\'s not that life is so short, it\'s just that you\'re dead a really long time.";
        break;
    }
    case 19:
    {
        hint = "Those who love sausage, the law, and holy canon should never watch any of them being made.";
        break;
    }
    case 20:
    {
        hint = "What if there were no hypothetical questions?";
        break;
    }
} // End of switch
hint = "HINT: " + hint;

This code is for a dynamic tet with the var 'hint' (No ' ' )

But in as3 there is no var and I am having trouble converting it to AS3 coding, can someone show me the correct conversion of the code to make it work with AS3. I will make the instance name 'hint'.

Thank you ahead of time.

This topic has been closed for replies.

2 replies

Inspiring
March 12, 2010

In addition to what other guys said, if you still want to use switch it should be:

switch (Math.round(Math.random() * 20)) - this will cover all the possible integers.

Nevertheless, I believe using switch in this case is not as efficient and scalable as using, say, array.

1. array index access is faster than switch.

2. if you need to make additions, you will need to make additions in switch while with array you just need to add/subtract member

3. if you will need to switch to a more dynamic model (say, getting hints from xml) - it will not be possible with switch

4. you may need a greater degree of randmization (Math.random is not as random as one may think) in which case resorting/reordering array is much simpler and faster.

So, I suggest you switch to the following code:

var hints:Array = [];
hints[0] = "Anticipation of death is better than death itself.";
hints[1] = "Anything worth doing is worth overdoing.";
hints[2] = "At the core of all well-founded belief lies belief that is unfounded.";
hints[3] = "Beware of geeks bearing gifs.";
hints[4] = "Collaboration is essential: It allows you to blame someone else.";
hints[5] = "Don\'t worry, this will take about as long as the time it takes to play a 2v2 match with 4 ret pallies.";
hints[6] = "If you are not part of the solution, you are part of the precipitate.";
hints[7] = "If you experience poor performance, adjust the quality settings in your preference menu option.";
hints[8] = "It\'s not that life is so short, it\'s just that you\'re dead a really long time.";
hints[9] = "Never let your schooling interfere with your education.";
hints[10] = "Read my MIPs - no new VAXes.";
hints[11] = "The heresy of today is the logic of tomorrow.";
hints[12] = "The problem with instant gratification is that it takes too long.";
hints[13] = "The total amount of confusion in this world remains constant. It just gets shifted around.";
hints[14] = "There is more to life than increasing its speed.";
hints[15] = "There is nothing worse than the brilliant image of a fuzzy concept.";
hints[16] = "Those who love sausage, the law, and holy canon should never watch any of them being made.";
hints[17] = "Two wrongs don\'t make a right, but three lefts do.";
hints[18] = "What if there were no hypothetical questions?";
hints[19] = "Where is the sea, said the fish, as they swam through it.";
hints[20] = "You can safely blame any bugs on Aoedipus, who is too busy grinding battlegrounds to do QA on her own work.";

hint = hints[int(Math.random() * hints.length)];

// OR

hint = hints[Math.round(Math.random() * (hints.length - 1))];

Inspiring
March 11, 2010

a cursory view demonstrates to me no errors in your code save one point ..

Math.Random returns a value less then 1 .. soooo .. some of your "cases" will never be true

sith717Author
Participant
March 11, 2010

So will this code work with AS3 swf file even tho it its made for a AS2 swf file?

Thats what I am asking, how to make it work with as3, if it already will that is...

Inspiring
March 11, 2010

sure .. but as I said .. Math.random

returns a value less then 1 so only 2 cases are possible 1 & 2 ..