Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.

## How do i make a sprite stop following another? Moderators: adafruit_support_bill, adafruit

How do i make a sprite stop following another?

So, basically, i'm making this game where there is a slime, and when the player gets close to the slime, the slime starts following the player. The slime has a passive wandering AI, but it just never returns to that AI when it starts following the player.

So, what i want to do, is make it wait a bit after it's far away, then return to wander AI, but the problem is, i have no idea how to make it stop following the player.

In short, i need a way to make the slime stop following the player.

Any help is greatly appreciated, thanks in advance!

HiroyuYulin

Posts: 1
Joined: Thu May 13, 2021 1:43 am

Re: How do i make a sprite stop following another?

It's a classic state-machine problem. There are two states, wandering and chasing, and there are conditions to move between these.
So we need a variable that defines the state. Say we call it Slimestate. You decide the exact conditions. For example 1=wandering, 2=chasing.
Now you make your blocks to have an endless loop, within them an if then-else. If state=1 then it does the wandering code, if it is 2 it does the chasing code.

Then you go to the conditions. If it is close, it has to go into chasing mode. But it should only do that once: so we need something to monitor when this "collision" happened. Say we introduce a variable "chasetime". We make it 0 on boot.

In block-code you can make a separate "watchdog". It has to do the following (pseudocode):
Code: Select all | TOGGLE FULL SIZE
`On startLoop forever    If slime is nearby AND chasetime=0 then        slimestate becomes 2 (chasing mode)        chasetime becomes 300 (frames)*        easetime becomes 60 (I explain this part later)`

* check how fast a routine is called, I guess 30 or 60 times a second.
You make a second watchdog that subtracts 1 from chasetime if it is greater than 0 and that when it is 0 the slimestate becomes 1 again.
Code: Select all | TOGGLE FULL SIZE
`On startLoop forever    if chasetime>0 then       subtract 1 from chasetime    if chasetime=0 then       slimestate=1`

Now there is a possible situation that messes up: if slime is still nearby, it will immediately jump to chasemode once more. So we also need easetime.
On start make easetime=0
So if me make chasetime 300, we also make easetime 60 (2 seconds to escape).
The second watchdog has to be altered a bit:
Code: Select all | TOGGLE FULL SIZE
`On startLoop forever    if chasetime>0 then       subtract 1 from chasetime    If chasetime=0 then         if easetime>0 then            subtract 1 from easetime            slimestate=1 (wander)`

blnkjns

Posts: 624
Joined: Fri Oct 02, 2020 3:33 am