• 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!

Finished Asteroids tutorial, want to have a chance to spawn more asteroids

C

Code-E

Guest
Hey there,

Hope I'm posting this in the right place.

I've just gone through the tutorial for the Asteroids style game, and just to get a little extra practice I want to keep adding to it.
I've had the thought to make it so that every time an asteroid is destroyed there's a chance that more will spawn.

Buut I haven't been able to work out how to do that. My current guess is an 'if' statement inside the destroy event's code. Just not sure how to go about it.
I looked through the help doc, but wasn't sure where to start.

Thanks for any help!
 
A

arhgames

Guest
Hi Code-E

in the 'collision with bullet' event, or whichever event executes instance_destroy(), try putting this instead:

Code:
var chance = XX
var a = random(100)
if (a<=chance)
{
instance_create(random(room_width),random(room_height),obj_asteroid)
}

instance_destroy()
you can change the variable "chance" to whatever % chance you want for a new asteroid to spawn (replace XX with any number between 0 and 100). If 100, then the variable "a" will always be less than or equal to "chance" and the creation code will always happen. If "chance" = 0, then it will never happen.

hope this helps!

Edit: just realised you may have meant "smaller" asteroids coming out from the destroyed one. My bad! leaving this code here anyway.
 
Last edited by a moderator:

kburkhart84

Firehammer Games
Want a bit of a learning experience??? Once you figure that out, make it to where the roids don't spawn exactly where the original one is, rather they spawn a given distance from the original, in smaller faster versions. The idea is that the bullet comes from a direction(say the left) and the spawned roids go towards the right, away from where the bullet hit the big asteroid, at random angles, but all away from the bullet collision.

That part will make you think about breaking the problem down. It doesn't take much math, and GMS has nice functions that can do all the calculations for you if you learn them.
 
C

Code-E

Guest
Hi Code-E

in the 'collision with bullet' event, or whichever event executes instance_destroy(), try putting this instead:

Code:
var chance = XX
var a = random(100)
if (a<=chance)
{
instance_create(random(room_width),random(room_height),obj_asteroid)
}

instance_destroy()
you can change the variable "chance" to whatever % chance you want for a new asteroid to spawn (replace XX with any number between 0 and 100). If 100, then the variable "a" will always be less than or equal to "chance" and the creation code will always happen. If "chance" = 0, then it will never happen.

hope this helps!

Edit: just realised you may have meant "smaller" asteroids coming out from the destroyed one. My bad! leaving this code here anyway.
That works perfectly, thank you!

I was pretty far off though. For some reason it didn't occur to me to put the code in the 'collision with bullet' event.
 
Top