A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: is there a way to make this less longwinded?

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Posts
    23

    is there a way to make this less longwinded?

    var n = Math.round(Math.random()*4+1);


    var x = 0;
    box1.onPress = function() {
    x = 1;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };

    box2.onPress = function() {
    x = 2;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };

    box3.onPress = function() {
    x = 3;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };

    box4.onPress = function() {
    x = 4;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };

    box5.onPress = function() {
    x = 5;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };

    box6.onPress = function() {
    x = 6;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box7.onPress = function() {
    x = 7;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box8.onPress = function() {
    x = 8;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box9.onPress = function() {
    x = 9;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box10.onPress = function() {
    x = 10
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box11.onPress = function() {
    x = 11;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box12.onPress = function() {
    x = 12;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box13.onPress = function() {
    x = 13;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box14.onPress = function() {
    x = 14;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box15.onPress = function() {
    x = 15;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box16.onPress = function() {
    x = 16;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box17.onPress = function() {
    x = 17;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box18.onPress = function() {
    x = 18;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box19.onPress = function() {
    x = 19;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box20.onPress = function() {
    x = 20;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box21.onPress = function() {
    x = 21;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box22.onPress = function() {
    x = 22;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box23.onPress = function() {
    x = 23;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box24.onPress = function() {
    x = 24;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };
    box25.onPress = function() {
    x = 25;
    if (x == n) {
    trace(true);
    } else {
    trace(false);
    };
    };

  2. #2
    maybe stick it in a for loop?
    I use flash mx 2004 pro

  3. #3
    Junior Member
    Join Date
    Apr 2004
    Posts
    23
    i know, but i'm not sure how to go about doing it.

  4. #4
    Member
    Join Date
    Apr 2004
    Posts
    82
    This would be shorter. Do the testing in a seperate function.
    code:

    box1.onPress = function() {
    test(1);
    }
    box2.onPress = function() {
    test(2);
    }

    ..

    function test(x) {
    if (x == n)
    trace(true);
    else
    trace(false);
    }


  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    In this method, the number you want to test with is stored as a variable called 'idx' attached to each movieclip. A single function compares 'idx' to n.

    code:

    var n = random(25)+1;

    textBox = function()
    {
    if (this.idx == n) {
    trace('true');
    }
    else {
    trace('false');
    }
    }

    // this sets up the indices and the onPress handling for each box
    for (var i = 1; i <= 25; ++i) {
    var mc = _root["box"+i];
    mc.idx = i;
    mc.onPress = textBox;
    }




    Secondly, you should be aware that using Math.round() with Math.random() will give you an unequal distribution of values. In the example you cited:

    Math.round(Math.random()*4+1);

    The value of Math.random()*4+1 ranges from 1.0 to 4.999... and you are rounding it, which gives you the following distribution:

    1 12.5%
    2 25%
    3 25%
    4 25%
    5 12.5%

    If you want the numbers from 1-n with equal distribution, use any of the following (which are all equivalent):

    Math.floor(Math.random()*n+1);

    Math.floor(Math.random()*n)+1;

    or

    random(n)+1;

    Also, if you get in the habit of numbering your movies to start at 0 instead of 1, then you can omit the +1 and simply use

    random(n);

    which is my all time favorite.

    Finally, is there a way to make me less long-winded? I think not.
    Last edited by jbum; 05-15-2004 at 05:36 AM.

  6. #6
    Junior Member
    Join Date
    Apr 2004
    Posts
    23
    lol. thanks guys =)

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