Is there a way to make a sprite stop moving a certain distance away from something?

Currently, I’m making a little game as a side project, and I’d like to implement a “range” feature. Currently, units simply move until they’re touching an enemy, then they attack until it’s dead. I’d like to add a function called “Range,” where if an enemy enters the range, they will be attacked instead of only attacking when colliding. I’d also like to do this for all sprites of the “Enemy” type to improve performance. Any help is appreciated! (The game is down below, just scroll over to the slime and press A.)

Since you’re working only with the distance to the right and left in your range, aka only the X coordinate, this is going to be really easy! You just need to figure out if the X coordinate of a given enemy is within a certain distance of wherever the center of the “range” is. The center of the range could be anywhere you want, but, looking at what you have so far, I think you would want the center to be at X = 0, putting it at the very left edge of the screen.

First, we have to find the difference between the X coordinate of the range “center” and the enemy X, like this:

Distance = EnemyX - RangeX

And then check if that distance is close enough to be considered “in range”, like this:

if (Distance < RangeDistance) {do something}

You don’t actually need to make a “Distance” variable! Instead, you could just put it all into one big IF block, like this:

If ( (EnemyX - RangeX) < RangeDistance) {do something}

If you want to, you don’t have to use a variable for “RangeX”. Instead, you can replace it with whatever number you decide should be the range center, as long as you don’t want that number to ever change during the game! You can do the same thing with “RangeDistance” if you want. I usually choose not to do this, personally, because, if I use variables and set them to the number I want at the very top of the OnStart block, I can easily change all of them in one place!

The last thing is to put that code into a loop that loops through and checks every enemy. Then you probably want to put that loop into a forever loop so that it runs forever!
To loop through every enemy, you can use a For loop. It would look kinda like this if you use blocks:

For (value) of (array of sprites of kind (enemy)) {
(The enemy checking code goes here)
}

You can find the For loop in the Loops tab and the “array of sprites of kind (…)” bit in the Sprites tab. To check every enemy, you have to replace the “enemy(X)” part in the checking code with a “value(X)” bit. You have to drag the small “value” bit from the top of the For loop into that to get it there. That thing is basically a variable, but you can only use it inside the For loop, which you can tell because it’s slightly smaller than normal variables and there’s no drop-down menu to change the variable.

Just one more thing: with the current code, any enemy left of the range center will be counted as “in range” because EnemyX - RangeX will always be negative, and, therefor, less than RangeDistance. To fix this, you have to find the abs() thing in the Math tab (it might be inside one of the drop-down menus) and use it to surround the “EnemyX - RangeX” bit of code. This will make sure that, if it is negative, it will be converted into the “absolute value”, which just means that it takes away the “negative” and forces the number to be positive.

Sorry if none of this makes sense. I really like encouraging people to figure it out themselves instead of just sending someone a bit of code which they can copy, which means they learn less IMO. I suppose this is still copying code, but whatever. If you really want me to just send the code, I’ll do that, but try to do it yourself first!!