-
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);
};
};
-
maybe stick it in a for loop?
-
i know, but i'm not sure how to go about doing it.
-
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);
}
-
Senior Member
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.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|