Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hopefully this should be a simple one; I want to restrict the points that instances enter the screen from so they don't come in at the edges.

In Game Maker I'm using the following code instance_create(random(room_width), random(-100) - 50, obj_enemy1); to create the instance off screen (create(x, y, ...))

At the moment I'm just using the room_width to define the max width for the random on x, but ideally I want to find a way of defining a max AND min width for the random. I can't figure out how to restrict the range on the x axis to between say 100 and 350.

Any help would be appreciated.

Cheers

share|improve this question
    
Hi guys, I've been tinkering with this this morning and have created this code, but it's still not working minrand = 150; maxrand = room_width - 150; randnumber = random(room_width); realrand = 0; if randnumber < minrand {realrand = 150} else {realrand = room_width - 150}; instance_create(realrand, random(-100) - 50, obj_enemy1); timer -= 10; alarm[0] = timer; randnumber = 0; still no luck :( –  CLockeWork May 25 '13 at 9:22
    
If you need a random number between a and b, you should just do: number = a + random(b - a). –  Sam Hocevar Jul 25 '13 at 21:45

3 Answers 3

after some poking around I've found a working solution, if anyone can suggest an easier way I'd greatly appreciate it.

//adds extra planes to the room

//create a collection of temp variables to difine the range as a min and max
//create a random number variable based on the room size
//find the location of the random value and if it falls outside of the min or max
   //move move it to the min or max
//create an enemy plane within the restricted x axis of the room and between 50 and
   //100 pixels above the screen

var minrand, maxrand, randnumber, realrand;
minrand = 50;
maxrand = room_width - 50;
randnumber = random(room_width);
realrand = 0;

if randnumber < minrand
{
realrand = minrand
}
   else
   if randnumber > maxrand
   {
   realrand = maxrand
   }
    else
    {
    realrand = randnumber
    };


 instance_create(realrand, random(-100) - 50, obj_enemy1);

Thanks

share|improve this answer
1  
Game Maker Language also has this function: random_range –  Seth Battin May 30 '13 at 4:20

You can use the median function:

point = median(smallestValue, randomValue, largestValue)
share|improve this answer

Try this:

instance_create(random_range(50,room_width - 49), random(-100) - 50, obj_enemy1);

It does everything your script does, and it is more evenly balanced.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.