Translations

Know another language? Translate this tutorial!

Stats

6.7K visitors
13.3K page views
270 translation visitors
514 translation page views

Randomly spawn object at random or set intervals

Favourite 23 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!

Comments

5
tchem 3,890 rep

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

Friday, October 12, 2012 at 3:28:34 AM
6
SciFiStudios 3,240 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
1
jegcelorio 3,561 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
0
BurningWood 1,998 rep

Thank you :D

Wednesday, July 31, 2013 at 2:07:23 PM
0
BurningWood 1,998 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
0
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
0
dstrawberrygirl 352 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
0
mrnannings 1,213 rep

Short tut but good

Sunday, March 16, 2014 at 12:56:23 PM
0
luxvideo 186 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
0
Edelplastic 343 rep

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

Sunday, April 20, 2014 at 1:23:43 AM

Leave a comment

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