Can you make a sprite chase another sprite?



My student would like one of his sprites to chase another sprite. How would he do this?


I would like some help with this as well.


Does he want it to follow the other sprite’s trail or to always move in the direction the target sprite currently is?



Move in the direction that the target Sprite currently is. The idea is that the good guy is one Sprite and the bad guy is another Sprite and the bad guy should be trying to catch the good guy.


Here’s the simplest version of “chasing”, in which the ship chases the mouse cursor around the screen. The general idea is that you can get the angle right by finding the difference between the mouse cursor and the ship in both the x and y directions. As long as your x and y velocities maintain this same ratio, the ship will move in the correct direction. I used a 10:1 ratio (divided the difference in position by ten for the velocity) to ensure that the ship didn’t catch the cursor too quickly.

chase mouse

In this second version, I used trig to make the velocity constant. Notice that when deltaX is negative, the angle ends up in the second or third quadrant, so you’re going to get the wrong angle value. You have to correct by adding pi to the angle.

chase mouse constant velocity

In this one, I control the green ship with the arrow keys, and the orange ship chases me.

ship chase ship

Here, I pulled out the chasing into a function and added some colliders. That way the green ship can’t go off the screen, and it makes a sound when the orange ship catches up.

final chaser game


Hi. I helped one of my students with the third option and got the enemy sprite to follow his character. However, how would I add gravity to the enemy sprite? The code seems to overwrite the line that he uses to add gravity to the sprite: sprite.velocityY = sprite.velocityY + 0.1 does not seen to work with the sprite that is chasing; instead it completely defies it and just floats. Adding gravity is something he would like to do.

Thank you.



Hi, can you link to the project so we can get some more context? I don’t think we’ll be able to debug the code without seeing it.



Hi, I remix your work, the final chaser game and add lives. I intend to share it with my students to give them idea about creating a simple game. The game will start with 10 lives then every time the orangeShip will collide with the greenShip, the lives will be deducted by 1 and if the lives is 0, a text “GAME OVER” will be displayed on the screen and the ships will disappear.
The problem is, the text “GAME OVER” will be displayed but disappear and the lives will continue from decreasing its value and the sound won’t stop.
here are the codes that I have added:
if (orangeShip.collide(greenShip)) {
lives = lives - 1;
// play a sound if the orange ship catches you.
playSound(“sound://category_digital/laser_fade_2.mp3”, false);
if (lives == 0) {
lives = 0;
orangeShip.visible = false;
greenShip.visible = false;
text(lives, 250, 15);
text(“GAME OVER”, 200, 200);
Can you please help me so the game would play as I expect it to be.
Thank you.


Would adjusting your if/else fix this? Making it so only if lives is above 0 the controls work, and else (lives less then or equal to zero) show your GAME OVER screen.