If you remove the array, you will need to define a legit start and end-point for the search function.
This is not effective as an array, if you use a lot of sprites.
// Project: Next Sprite from Point
// Created: 2015-10-17
// set window properties
// although its easier to use the array code for better performance :/
SetWindowTitle( "Next Sprite from Point" )
SetWindowSize( 1280, 720, 0 )
// set display properties
SetVirtualResolution( 1280, 720 )
SetOrientationAllowed( 1, 1, 1, 1 )
type searchtype
DIS# as float
num as integer
endtype
// the coordinates for the search sprite
search_x=640
search_y=300
//Insert sprite that will be ignored, not anymore
IgnoredSPR=createsprite(0)
setspritecolor(IgnoredSPR,0,0,255,255)
setspritesize(IgnoredSPR,10,10)
setspriteposition(IgnoredSPR,search_x+50, search_y+50)
setspritedepth(IgnoredSPR,1)
center=spr
for i=1 to 20
spr=CreateSprite(0)
SetSpriteSize(spr,32,32)
SetSpritePosition(spr,random(0,1280),random(100,720)) // position the current sprite at a random position
next i
start_sprite=10000 // this is the start position of the function finder (10000 is standard for AGK, right now)
end_sprite=10200 // this is the end position of the function finder, this should be managed by the amount of sprites you have created,
shortspr=find_nearest_sprite(search_x,search_y,start_sprite,end_sprite)
color1=MakeColor(255,0,0)
color2=MakeColor(255,255,255)
// GetDistance#( X1#, Y1#, X2#, Y2#)
do
print("Nearest Sprite, does also need a"+chr(10)+"start and endpoint for the search (btr use ary)")
DrawLine(search_x,search_y,getspritex(shortspr)+GetSpriteWidth(shortspr)/2, getspritey(shortspr)+GetSpriteHeight(shortspr)/2,color1,color2)
Print( ScreenFPS() )
Sync()
loop
function find_nearest_sprite(f_x,f_y,start_s,end_s)
dim searcharray[] as searchtype // only as big as the sprite register
for rep=start_s to end_s
if GetSpriteExists(rep)=1
searcharray.length=searcharray.length+1
i=searcharray.length
searcharray[i].DIS#=GetDistance#( f_x, f_y, getspritex(rep), getspritey(rep))
searcharray[i].num=rep
endif
next rep
// open this to watch into the list, copy it after the .sort() and look what changed.
remstart
do
for i=0 to searcharray.length
print(str(searcharray[i].num)+" "+str(searcharray[i].DIS#))
next i
sync()
loop
remend
searcharray.sort()
if searcharray.length>-1
outnum=searcharray[0].num
else
end
endif
undim searcharray[]
endfunction outnum
// distance code
Function GetDistance#( X1#, Y1#, X2#, Y2#)
Dist_X# = X1# - X2#
Dist_Y# = Y1# - Y2#
Distance# = Sqrt(Dist_X# * Dist_X# + Dist_Y# * Dist_Y#)
EndFunction Distance#