How do I make sprites move around the scene randomly without moving outside the screen On spawn

I need a sprite to move around the x and Y axis without leaving the screen. Please help

Hi, there are several ways how to do it but it also depends on what you actually want to make:

Do you want the sprite to bounce off the border?
Do you want it just to stop?
What about screen wrap (the object leaves the screen and appears on the other side)?


When it hits the border I want it to go in another random direction, keep moving around the map. No screen wrap. It’s a sorting game where these things move around the map and you have to grab/ sort them in time

Okay, so the movement mechanic is similar to the ball in a breakout game (the ball hits the border and bounces back).
Here is a tutorial for a breakout game:

Basically you need border sprites and the bounce extension on your moving sprite.

If you are not happy with this solution, there are several ways how to limit movement to the screen (there is also an extension for that) and to make the sprite move around. But checking the collision between border sprites and the moving sprite is probably the fastest way to get a good result.

If you would give more details about the way the sprites are spawned, which movement you expect, and the sprite shape etc. it may be easier to suggest a better solution.


The problem with that is that’s just one ball I am going to have multiple sprites going in multiple directions not touching each other

That’s true but it gives you the basic mechanic which you can apply to as many instances of the moving object as you like. For learning how things can be possibly done (most of the time there are many ways how to do things), I would recommend just to experiment a bit by making one object move. Then try to let two objects move around etc. In case you are stuck you will definitely get help in the forum.

When you you have several sprites moving around randomly they will touch each other from time to time. You could let them ignore each other or make them bounce as well.

For a possible solution you could start from here:

In this small example the border sprites are hidden (depending on your scene that’s maybe not necessary). The object ‘move’ goes in a random direction and bounces off the border sprite (but ignores other instances of its own). This works with all instances of the ‘move’-object because bounce is a behavior that is attached to this object.


Thank you, I appreciate it

1 Like