Translations

Know another language? Translate this tutorial!

Stats

4.7K visitors
50.9K page views
253 translation visitors
1,736 translation page views

Randomly spawn object at random or set intervals

Favourite 58 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.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

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

Comments

7
tchem 4,058 rep

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

Friday, October 12, 2012 at 3:28:34 AM
11
SciFiStudios 3,983 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
2
jegcelorio 3,723 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
1
BurningWood 2,919 rep

Thank you :D

Wednesday, July 31, 2013 at 2:07:23 PM
1
BurningWood 2,919 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
1
Dannyzigg 1,522 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
3
dstrawberrygirl 551 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
1
mrnannings 3,055 rep

Short tut but good

Sunday, March 16, 2014 at 12:56:23 PM
1
luxvideo 323 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
1
Edelplastic 685 rep

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

Sunday, April 20, 2014 at 1:23:43 AM
1
Albionix 209 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
0
ygy 152 rep

i need help respawning enemies after they die in android studio, i was wondering if anyone could help

Tuesday, July 12, 2016 at 5:50:06 PM

Leave a comment

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