Know another language? Translate this tutorial!


15.3K visitors
28.6K page views
464 translation visitors
882 translation page views

Randomly spawn object at random or set intervals

Favourite 39 favourites
Tutorial written by lonehangmanOriginally published on 21st, September 2012 - 3 revisions

If you're making something like a zombie survival game or just want random objects to spawn, you would find that making objects spawn randomly is a bit of a pickle.
But it's actually very simple, and can be applied to most games.

Firstly, in your layout, make a blank sprite, this is your spawner object. It can be what ever size you want. A player, and one instance of the object you are spawning (can be outside layout). Like so:

Now go to your event editor and try the following (The variable is for debug purposes, you can add it if you want):

What's happening

Every one second (this can be changed to any value, or even a random value ie. random 1-5, which will spawn at any time between 1 and 5 seconds) it will change the position of the spawner object to a random value on the map.
If the spawner object is not on screen it will spawn an object next to it and if it is on screen it will move again and try spawn something)

That's basically it!
If you are spawning zombies, you could make it that when they are created they move toward the player.

If there is anything I missed, feel free to drop a comment.

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!


tchem 4,048 rep

Cool......simple and objective...

Friday, October 12, 2012 at 3:28:34 AM
SciFiStudios 3,572 rep

Actually kind of pointless. No offense this would work but it's over kill if you just want them to spawn randomly. To get them to spawn randomly just make an event, every x seconds and add the action create object. Then set action parameters to...
Layer: Any layer
X: random(3000)
Y: random(3000)

Wednesday, December 12, 2012 at 9:29:32 PM
jegcelorio 3,713 rep

Great tutorial lonehangman!!

THanks SciFiStudios! Based on your comment I was able to do it using:
"System every random(2,4) seconds. System create Sprite on layer 0 at X.random(0,200), Y.random(0,200)"

Tuesday, March 26, 2013 at 2:16:46 AM
BurningWood 2,174 rep

Thank you :D

Wednesday, July 31, 2013 at 2:07:23 PM
BurningWood 2,174 rep

How do you stop the random items from spawning on top of solid objects the player cant reach?

Thursday, August 01, 2013 at 11:15:07 AM
Dannyzigg 1,365 rep

So when using either ScifiStudio's, or Lonehangman's, method you don't get much control in how random the spawn is , like if you wanted an it to spawn more often than another. Changing the seconds seems like it would very exponentially decreases the chance of it showing up. I am still rather new to this but I will work around with it and see if I can come up with something that gives you more control.

@Burningwood, again I am very new but best I can think of is to set the object to destroy when in collision with said unreachable objects.

Monday, September 23, 2013 at 12:30:46 AM
dstrawberrygirl 506 rep

I've been using sprites on a hidden layer with a series of image points spaced out over the surface of the sprite, then spawn an enemy at a random image point. For example, a rectangle with 9 image points, you spawn on the main / action / visible layer at image point random(0,8) - works well to control where enemies spawn in relation to obstacles.

Sunday, December 08, 2013 at 12:32:48 AM
mrnannings 2,315 rep

Short tut but good

Sunday, March 16, 2014 at 12:56:23 PM
luxvideo 318 rep

Very easy and simple to understand, when starting with this software, stuff like that is excellent for a starting point... from 30 mins of brain scratching, to 30 seconds of items spawning on the screen!! love it :)

Wednesday, March 19, 2014 at 1:57:25 PM
Edelplastic 609 rep

How to spawn a random bubble like in the Bust-a-Move game?

Sunday, April 20, 2014 at 1:23:43 AM
Albionix 204 rep

Hey everyone! I'm kinda stuck with the situation when I need to have the sprite to appear in several specific locations randomly with different frequency ( up to the certain point with 3 sec interval then less).
Any ideas?

Thanks a lot.

Thursday, July 03, 2014 at 6:58:56 PM
MEgoesBOOM 240 rep

thanks very helpfull

Monday, July 21, 2014 at 7:30:31 AM
nekoruru 250 rep

nice tutorial, but if i want to set and object only spawn 3 times only. how to do that? sorry im still new using C2.

Tuesday, August 12, 2014 at 4:48:04 AM
chrisinfinger 1,670 rep

nekoruru one way, (the way I would do it at my current Construct2 experience level)
would be to use a global variable to count how many instances of that enemy were made then stop.

ie. make a global called "numberEnemies" inititalized at start to 0 then in spawner code each time it spawns an enemy have it add 1 to numberEnemies var. Have a check for stopping when var is >= 3.

Friday, September 05, 2014 at 3:22:32 AM
MartiB 266 rep

Great tutorial. Thx!

Saturday, December 27, 2014 at 7:07:11 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.