Randomly Placing Sprites Off Screen

  • 16 favourites


2,679 visits, 4,268 views



This tutorial hasn't been translated.


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.

Your concept

So, you've got a top down shooter or another game that needs to generate a hoard of enemies to throw at your player, and to make it even more interesting you want to have those enemies come at your player from multiple directions.

You think to yourself: "Self, I just need to randomly place those sprites around my layout and send them after the player."

Your Problem

So, you set about spawning your sprites and it works...until one spawns right on top of you causing you to mess yourself and die. After cleaning out your shorts you realize that you need to make sure that all spawned sprites need to be placed off screen. (You come up with this after discarding the idea of changing all of your enemies to ninjas.)

Your Solution

A quick, neat and easy way is to randomly place your sprites and then checking to see if they happened to have been placed on screen. If so randomly place them again. This can be achieved with the while loop...add an event for when your enemy sprite is created as such:

You can see this code at work here, and you can get the capx (R117) from here.


Now you can throw as many enemies at your player as you wish without having to spend a lot of time explaining how your evil creeps spent years of training perfecting the stealthy arts of the ninja. (unless, of course, they ARE ninja...)


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