A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: ARGH! Need help with if else statement!

  1. #1
    plays with barbies
    Join Date
    Nov 2005
    Posts
    21

    ARGH! Need help with if else statement!

    Here is a link to the flash:

    http://www.theweblands.com/coloraccess.html

    The flash script is getting a random number sent to it from the server, this could probably all be done in flash, BUT: A I don't know how to do that. and B, I've already got the random number on php.

    on the first frame of my flash movie I have this if statement:

    // load the variable called "randomNumber" from the php page.
    // The random number is a number from 1 to 6.

    loadVariables('http://www.theweblands.com/getcolor.php', "");

    // change the name of the variable, why? because I thought maybe it needed to be done.

    random01 = randomNumber;

    // This statement should assign a color to a number and then show that number inside the dynamic text box so the user knows what color to pick.

    if(random01==1){
    mycolor = "red";
    }else if(random01=="2"){
    mycolor = "yellow";
    }else if(random01=="3"){
    mycolor = "gray";
    }else if(random01=="4"){
    mycolor = "black";
    }else if(random01=="5"){
    mycolor = "purple";
    }else if(random01=="6"){
    mycolor = "green";
    }

    What happens with this script is nothing. However, if I use just one = then the script will only run the first if statement and skip the rest.

    I read somewhere that there has to be two equal signs, and I've tried every combinations of random01==6 or random01=='6' and now "6" but nothing works. It just skips the entire statement.

    Anyone know what the problem is? besides my lack of actionscripting experience? I know I could probably make an array, which is my next step.

  2. #2
    Senior Member Branny's Avatar
    Join Date
    Feb 2001
    Location
    Manchester/UK
    Posts
    512
    try forcing a string match

    random01 = String(random01);

    if(random01=="1"){
    mycolor = "red";
    }else if(random01=="2"){
    mycolor = "yellow";
    }else if(random01=="3"){
    mycolor = "gray";
    }else if(random01=="4"){
    mycolor = "black";
    }else if(random01=="5"){
    mycolor = "purple";
    }else if(random01=="6"){
    mycolor = "green";
    }

    remember to have all your arguments in quotes, your original script was missing the quotes off the first argument.

  3. #3
    Knowledgable n00b BlueGeek's Avatar
    Join Date
    Nov 2006
    Location
    New York City
    Posts
    137
    and just to be efficient (cause going to PHP for random numbers in Flash isn't) Math.random() returns a random number between 0 and 1, so multiply by 5 and you have a random number between 0 and 5, add 1 and you have a random number between 1 and 6, and then cast to an int and you have a random integer between 1 and 6:

    Code:
    random1 = (integer)((Math.random()*5)+1);
    as far as the if statement goes, since all of your random1 at this point SHOULD be integer, use NO quotes:

    Code:
    if(random01==1){
    mycolor = "red";
    }else if(random01==2){
    mycolor = "yellow";
    }else if(random01=="3"){
    mycolor = "gray";
    }else if(random01==4){
    mycolor = "black";
    }else if(random01==5){
    mycolor = "purple";
    }else if(random01==6){
    mycolor = "green";
    }
    leaving us with the final code:


    Code:
    random1 = (integer)((Math.random()*5)+1);
    
    if(random01==1){
    mycolor = "red";
    }else if(random01==2){
    mycolor = "yellow";
    }else if(random01=="3"){
    mycolor = "gray";
    }else if(random01==4){
    mycolor = "black";
    }else if(random01==5){
    mycolor = "purple";
    }else if(random01==6){
    mycolor = "green";
    }

    EDIT: to be tidy,if you haven't already defined random1, you should replace the first line (random1 = ...) with:

    Code:
    var random1 = (integer)((Math.random()*5)+1);
    "...there were only two good books in the hotel gift shop and I had written both of them."
    -Douglas Adams

  4. #4
    Knowledgable n00b BlueGeek's Avatar
    Join Date
    Nov 2006
    Location
    New York City
    Posts
    137
    Quote Originally Posted by Binxalot
    What happens with this script is nothing. However, if I use just one = then the script will only run the first if statement and skip the rest.
    the reason for that is because when you use ONE = sign, all you're actually doing is setting random1=1, any instance in Flash, C++, Java, etc where a variable is on the left side of a single = sign, sets that variable equal to what's on the right, even in if statements. This then returns a true value (which is default in the case that an expression doesn't resolve into a real true or false value) so the if statement becomes true, and it executes the code inside of it, and since it went to the first if statement, the other else's never get called
    "...there were only two good books in the hotel gift shop and I had written both of them."
    -Douglas Adams

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