How to make object spawn randomly only at certain parts?[solved]

A

Artwark

Guest
So I have two powerups that spawn randomly and this is how they are spawned

Create Event

Code:
randomize();
instance_create(irandom_range(50,room_width),irandom_range(50,room_height),obj_shield);

randomize();
instance_create(irandom_range(50,room_width),irandom_range(50,room_height),obj_powerup);
The problem with this is that they spawn near the edges and halfway outside the room and they also spawn at the score and pause menu which is disorienting.

I want it so that they will spawn only at these points but its random. How do I do this?
 
D

Dudeidu

Guest
You should only use randomize once at the start of the game.
If you don't want your object to spawn at random then dont use the random functions, or set a more specific range.
 

NightFrost

Member
If you don't want the powerups spawning near edges, you need to restrict your irandom ranges more. As they stand, they already never spawn at the fifty topmost and leftmost pixels, but can spawn by right and bottom edges. If you substract fifty from room_width and room_height in those create commands, they'll always spawn towards the center. Not spawning atop score depends on where it is displayed, and pause menu... should they not be spawning while pause menu is open anyway?
 
J

jr carey

Guest
what is random range? ive never heard of that function....but it looks to me like you don't have a try and catch in your code,
but there's a couple of ways to go about this, one would to take the code and say to only "create random at these positions" or "create where-ever but destroy them at these positions"
for now we`ll go over the simplest and most efficient one which is the first one which you attempting, I don't know what other locations you want to avoid, but ill help you get them away from the edges:

randomize();
instance_create(irandom_range(50,room_width),irandom_range(50,room_height),obj_shield);

when I ran your code I did get some sticking outside but the reason is obvious and help you with it

the reason their sticking outside is because you didn't take into account of the objects origin, when you say "hey put them anywhere you want within this area" its going to place them down
by origin, so to make game maker respect your requests you have to tell it to subtract the origin of that sprite, so for example if your shields are 32x32 pixels the origin is 16x16,
an easy way to find the origin is to either divide the sprite width and height by 2 or when you done creating the sprite click "center" and this will give you the origin location
now, that you know you have to subtract it, lets put it in the code:

randomize();
instance_create(irandom_range(50,room_width-16),irandom_range(50,room_height-16),obj_shield);

every time I ran this I never did get one sticking half way out of the room, because its no longer placing the objects by their origin, its subtracting their origin from the equation, that's what
the "-16" is, the object I used was 32x32, so I just subtracted the origin from the room_width and the room_height
 
Top