4 Replies Latest reply on Oct 9, 2009 1:40 AM by DraconianDevil

# How to set random math?

Ok I am making a very simple flash game for a class. I have all the coding working but I was wondering if any of you know how to do a certain thing.

Where the powerup(green box) falls out of the sky I have it set to when ever you hit it it will go *2+100 of the current x possition. While playing it is easy to catch the pattern and rack up points. I want to make it so that every time you hit the power up it will drop from the sky next time at a random place on the x axis.

I use action script 2.0.

Here is my code(look at the very bottom I will have a place in bold):

//tells score to start at zero
var score = 0;

//powerup starting position
Powerup1._x = 150
Powerup1._y = 20;

//begining of moving code
onEnterFrame = function() {

//tells power up to move down the y axis
//and go to the top of the screen if
//goes out bottom
Powerup1._y = Powerup1._y +6;

if(Powerup1._y > 400) {
Powerup1._y = 0;
}

//commands that make hero re appear
//on screen if goes off
if(Hero1._y > 400) {
Hero1._y = -100;
}
if(Hero1._y < -100) {
Hero1._y = 400;
}

if(Hero1._x > 550) {
Hero1._x = -70;
}
if(Hero1._x < -70) {
Hero1._x = 550;
}

//code to make clouds move
cloud1._y = cloud1._y +2.5;
cloud2._y = cloud2._y +4;
cloud3._y = cloud3._y +1.8;

cloud1._x = cloud1._x +1.2;
cloud2._x = cloud2._x +1.8;
cloud3._x = cloud3._x +2;

//code to make clouds not go off screen
if(cloud1._y > 400) {
cloud1._y = 0;
}
if(cloud1._y < 0) {
cloud1._y = 400;
}

if(cloud1._x > 550) {
cloud1._x = 0;
}
if(cloud1._x < 0) {
cloud1._x = 550;
}

if(cloud2._y > 400) {
cloud2._y = 0;
}
if(cloud2._y < 0) {
cloud2._y = 400;
}

if(cloud2._x > 550) {
cloud2._x = 0;
}
if(cloud2._x < 0) {
cloud2._x = 550;
}

if(cloud3._y > 400) {
cloud3._y = 0;
}
if(cloud3._y < 0) {
cloud3._y = 400;
}

if(cloud3._x > 550) {
cloud3._x = 0;
}
if(cloud3._x < 0) {
cloud3._x = 550;
}
//controls for up left right and down
if(Key.isDown(Key.UP)){
Hero1._y = Hero1._y -6;
}
if(Key.isDown(Key.DOWN)){
Hero1._y = Hero1._y +6;
}
if(Key.isDown(Key.RIGHT)){
Hero1._x = Hero1._x +6;
}
if(Key.isDown(Key.LEFT)){
Hero1._x = Hero1._x -6;
}

if (Hero1.hitTest(Powerup1)) {
Powerup1._x = Powerup1._x *2+100
Powerup1._y = 400;
score = score + 1;
scoreDisplay.text = score;
}

if (Powerup1._x > 400){
Powerup1._x=10
}
}

END CODE

The big code is the script I am using to make the powerup box move each time you score. What I want to do is make it random instead of *2+100, and if possible make it random within a range. The range I want is between x 5 and x 545 on the x axis.

Please tell me what I need to do to make this happen.

Dakota

• ###### 1. Re: How to set random math?

for the range you said you want, the random part of the equation would be 5 + Math.random()*540.

I do not know for sure what you intend when you include the x's next to the numbers

• ###### 2. Re: How to set random math?

Hello Dakota LBG, if you want to create a random number between two numbers, the formula is:

Math.round(Math.random() * (Highest - Lowest) + Lowest);

this gives you a random number between your highest and lowest wanted numbers..

in your case, it will be:

Math.round(Math.random() * (545 - 5) + 5);

may be you meant x next to 5 and 545 as an x axis element. but it doesnt matter.

you can just position your power block now by this random, like this:

Powerup1._x = Math.round(Math.random() * (545 - 5) + 5);

Hope this helps!

Cheers.

• ###### 3. Re: How to set random math?

Worked like a charm,

Thanks!

• ###### 4. Re: How to set random math?

glad I could be of some use!

you are welcome!