-1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 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 :( \$\endgroup\$
    – CLockeWork
    May 25, 2013 at 9:22
  • 1
    \$\begingroup\$ If you need a random number between a and b, you should just do: number = a + random(b - a). \$\endgroup\$ Jul 25, 2013 at 21:45

4 Answers 4

1
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Game Maker Language also has this function: random_range \$\endgroup\$ May 30, 2013 at 4:20
1
\$\begingroup\$

You can use the median function:

point = median(smallestValue, randomValue, largestValue)
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

I think you may have overcomplicated a few things in your code.

So assuming you want enemies to come in, say, from the top of the screen downwards, then you can achieve spawning a single enemy with one line of code:

instnace_create(view_xview[0]-enemy.sprite.width+irandom(view_wview[0]),view_yview[0]-enemy.sprite.height,obj_enemy);

(This code assumes that you have views enabled and are using view 0, and that the origin point for the object's sprite is set at (0,0))

(Replace enemy.sprite.height and enemy.sprite.width with whatever the height and width of the enemy's sprite is. If the sprite_index is the exact same sprite for what's being shown on screen then I'd advise using sprite_get_width(obj_enemy.sprite_index))

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .