Spawn Objects Randomly without Overlapping

3

Attached Files

The following files have been attached to this tutorial:

.capx

random-overlapping-tutorial.capx

Download now 172.97 KB

Stats

7,677 visits, 10,920 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 4 Jan, 2019. Last updated 19 Feb, 2019

Doing things in a random manner is somewhat easy to do, but to manipulate its function is hard to do especially when things being random tend to somewhat be out of control. In this tutorial, I will teach you how to spawn objects without overlapping to each other. This method is very simple and beginner friendly. So let's get started.

Code

Here's what we are trying to achieve:

-Spawning Events

-Overlapping Events

Spawning Events

I am using a For loop, you can use any other method of spawning multiple objects in your game. On system, use 'Create Object' then choose the object you are about to spawn, for the x and y use the random expression with the parameter of (0, Layout.Width) for x and (0, Layout.Height) for y. And make sure it is within the 'On Start of Layout' Event.

I am also spawning the Player on random.

Overlapping Events

For this event, when object of your choosing is overlapping with the same object, set position by (0, Layout.Width) for x and (0, Layout.Height) for y. Make sure it is outside of the 'On Start Layout' Event.

I've also add one for the player, make sure the event for the player is within the 'On Start of Layout' Event to prevent the object of your choosing to set another position when colliding it with the player.

Explanation

Whenever the object of your choosing is overlapping with that same object, it will set a new position at random hence the 'random' expression. You can however do this with the 'choose' expression as it works the same way expect one time only, alternatively you can put the event on the 'On Start of Layout' Event for it to do it once.

Conclusion

This is a very simple method to execute as it contains simple code for a beginner to work on. Although it has some issues it is merely a minor one that can easily be fix as this tutorial focus on the method and concepts.

.CAPX

random-overlapping-tutorial.capx

Download now 172.97 KB
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!