A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Random guessing game - actionscript error

  1. #1
    Junior Member
    Join Date
    Oct 2004
    Posts
    5

    Random guessing game - actionscript error

    Hi, im new to these forums. I'm making a guessing game - when you press one of three buttons, this script executes:

    code:

    on (release)
    {
    _root.win=0;
    _root.win=random(2);

    if (_root.win=0);
    { gotoAndStop(3); } //Lose

    if (_root.win=1);
    { gotoAndStop(4); } //Win
    }



    The problem is that the player always wins. Where is the error?
    Last edited by jbum; 10-09-2004 at 02:12 PM.

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Hi,

    couple of mistakes in your code, specially the needed double eq. sign in if statement:

    code:
    on (press) {
    _root.win = 0;
    _root.win = random(2);
    }
    on (release) {
    if (_root .win==0 ) {
    this.gotoAndStop(3);
    } else if (_root .win==1) {
    this.gotoAndStop(4);
    }
    }



    gparis

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    The problem is your use of a single equals sign in the if statement. There are two similar looking operators

    = (assignment)
    == (equality)

    When you are testing for equality, such as in an if statement, you need to use the second one (which is two equal signs). Otherwise, it assumes you want to assign the value on the right side to the variable on the left side.

    Also, you should omit the semicolons after the if statement, they will cause it to execute incorrectly.

    It's

    if (cond) statement; or
    if (cond) { block; }

    not

    if (cond); statement; and not
    if (cond); { block }

    Here's are some rewrites of your code which simplifies some other things.

    code:

    on (release)
    {
    _root.win=0;
    _root.win=random(2);

    if (_root.win==0)
    {
    gotoAndStop(3);
    } //Lose
    if (_root.win==1)
    {
    gotoAndStop(4);
    } //Win
    }




    Simplification #1. Use the 'else' clause.

    code:

    on (release)
    {
    _root.win=0;
    _root.win=random(2);

    if (_root.win==0)
    {
    gotoAndStop(3);
    } //Lose
    else
    {
    gotoAndStop(4);
    } //Win
    }



    Simplification #2. Omit the variable.

    code:

    on (release)
    {
    if (random(2)==0)
    {
    gotoAndStop(3);
    } //Lose
    else
    {
    gotoAndStop(4);
    } //Win
    }




    Simplification #3. Use the random value to compute the frame number. This is clever, but not particularly easy to understand - I would probably use the previous method.

    code:

    on (release)
    {
    gotoAndStop(3 + random(2));
    }



    - Jim

  4. #4
    Junior Member
    Join Date
    Oct 2004
    Posts
    5
    Thank you so much!
    You guys really helped a newbie out!

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