I guess that the purpose of this code is an enemy's constant speed approach to a player.
The denominator of this code stands for the distance between a player and an enemy. When divide by zero error occurs, an enemy has already collided with a player.
Write as follows in order to kill divide by zero error.
D=SQR(POW(X2-X,2)+POW(Y2-Y,2))
IF D>0 THEN X2=X2+(X2-X)/D:Y2=Y2-(Y2-Y)/D
The following code is another solution.
D=SQR(POW(X2-X,2)+POW(Y2-Y,2))
IF D>0 THEN
A=ATAN(Y-Y2,X-X2)
X2=X2+COS(A):Y2=Y2+SIN(A)
ENDIF
thank you very much btw!
but i have one more question, i would like to make the enemy sprite follow my sprite realistically, i have done this before but it dosent seem to be working now.
so basically the enemy sprite homes in on the middle of the screen where the player is.
I overlooked suspicious points.
Could you replace
X2=X2+(X2-X)/D:Y2=Y2+(Y2-Y)/D
with
X2=X2+(X-X2)/D:Y2=Y2+(Y-Y2)/D
?
An enemy will LEAVE a player with the former code.