• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM How to spawn falling objects at random points from the ceiling? [SOLVED]

pixeltroid

Member
I'm trying to set up a boss fight where debris falls from random points from the ceiling (a pretty common thing in 2D platformer games).

I have the debris object ready "obj_debris" (with gravity and an explosion sequence when it hits the ground or the player). And I need them to appear and fall within a time slot in the bosses timeline. And they need to originate along the top border of my in-game VIEW.

Any ideas as to how I implement this?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
For timed releases your best bet is usually to use an alarm. So, in the boss object set an alarm to however many steps you want for the first object to fall, eg: 120 = 2 secs if the game speed is 60fps. Then in the alarm, create the fall object and reset the alarm. For the actual creation, use the function camera_get_view_width() and camera_get_view_x/y to get the position of the camera, then use random() to create the instance, eg:
GML:
var _w = camera_get_view_width(view_camera[0]);
var _x = camera_get_view_x(view_camera[0]);
var _y = camera_get_view_y(view_camera[0]);
instance_create_layer(_x + random(_w), _y - 32, layer, OBJECT);
 
I'd set an alarm, and then every time the alarm passes, add the instance at a random x position using display_get_gui_width to determine the width of the game window?
GML:
var scrn_width = display_get_gui_width();
instance_create_layer(irandom_range(0, scrn_width), 0, "Object_Layer", obj_debris);
 

pixeltroid

Member
I'd set an alarm, and then every time the alarm passes, add the instance at a random x position using display_get_gui_width to determine the width of the game window?
GML:
var scrn_width = display_get_gui_width();
instance_create_layer(irandom_range(0, scrn_width), 0, "Object_Layer", obj_debris);
Hey. I use GM 1.4. I don't have the function "scrn_width" you mentioned šŸ˜
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
@Nocturne I dont have view_camera and camera_get_view_width in my copy of gamemaker. Is there some other function I can use instead of those?
Just use the same code, only swap out the GMS2 functions for view_wview[0] and view_xview/yview[0]. :)
 

pixeltroid

Member
Just use the same code, only swap out the GMS2 functions for view_wview[0] and view_xview/yview[0]. :)
I did this and its working.

var _w = view_wview[0]
var _x = view_xview[0]
var _y = view_yview[0]
instance_create(_x + random(_w), _y - 32, obj_debris );


It's working. Thanks for your help!
 
Top