The easiest way is to not use SET SPRITE PRIORITY and let Darkbasic Pro do the priority the way does it naturally.
The old natural way was by the sprite number but with the newest update sprites are drawn by the image number used in the sprite.
Sprites that use image number 5 will always be drawn in front of sprite image numbers 1 to 5 and behind sprite image numbers 6 and up.
The following code snip creates 17 sprites with randomly created sprite numbers (shown at the top) and 1 to 17 image numbers (shown at the bottom) that are also randomly picked. It does it that way to show that it doesn't matter when the sprite or images are created... they will always be in the order of the image number used in the sprite.
One sprite will follow the mouse with an image number that can be changed by hitting the spacebar. Once the image is changed move the box (via the mouse) close to the other sprites on the screen and you'll notice (based on the image number) it'll go above any sprite at or lower than the image number and be under any image number above the current image number.
randomize timer()
` Create 17 sprites
for t=1 to 17
` Pick a random sprite number between 1 and 999
repeat
SpriteNum=rnd(998)+1
until sprite exist(SpriteNum)=0
` Pick a random image number between 1 and 17
repeat
ImageNum=rnd(16)+1
until image exist(ImageNum)=0
` Create a box
ink rgb(rnd(255),rnd(255),rnd(255)),0
box 0,0,50,50
ink rgb(255,255,255),0
` Show sprite number on the box
center text 25,0,str$(SpriteNum)
` Show image number on the box
center text 25,30,str$(ImageNum)
` Grab the image
get image ImageNum,0,0,50,50,1
` Create the sprite
sprite SpriteNum,t*40-40,t*10,ImageNum
next ImageNum
hide mouse
` Set the mouse for the highest image number
MImage=17
tim=timer()
do
` Show instructions
center text 320,300,"( PRESS SPACEBAR TO CHANGE MOUSE IMAGE NUMBER )"
` Show the mouse
sprite 1000,mousex(),mousey(),MImage
` Check for spacebar and if the timer is up
if keystate(57) and timer()>tim+200
` Pick a random image number
MImage=rnd(16)+1
` Reset timer
tim=timer()
endif
loop