I don't know what it's changing its image to, as I only see one image being loaded. Also, I'm not sure the purpose of writing out to a file. This code only creates a single sprite.
spr and
img don't appear to be defined anywhere either. Did you make those global variables and assign them somewhere else?
Let me see if I can help structure a better framework for ya.
In this example, I used the built-in sprite collision. Since it seems to ignore transparency and simply uses bounding boxes, you could replace this with a simpler mathematical approach. I also avoided using arrays to simplify the example.
Simply press the spacebar to load a new sprite to the screen. You can drag sprites around with the mouse.
REM Variables ////////////////////////////////
#CONSTANT SPR_CURSOR = 6000
Global _ImgIdx = 0
hide mouse
REM Load images //////////////////////////////
load image "c:\cursor.png", 6000
sprite SPR_CURSOR, 0, 0, 6000
// Set cursor sprite's priorit so it always appears on top
set sprite priority SPR_CURSOR, 1
sync on
REPEAT
cls
// Move cursor sprite with mouse
sprite 6000, mousex(), mousey(), SPR_CURSOR
// Press space key to load a new sprite
// Flag variable keeps from loading multiple sprites at a time
// if space key were to be held down.
if spacekey() = 1 and flag = 0
flag = 1
importSprite("c:\hfavatar.png")
endif
if spacekey() = 0 then flag = 0
if mouseclick() = 1
// if no sprite has been selected yet
if selectID = 0
// Loop through sprites backwards, because we want to detect
// collision with what appears to be the topmost sprite first
for i = _ImgIdx to 1 step -1
// Check for collision with cursor
if sprite collision(SPR_CURSOR, i) = 1
// On initial contact with sprite, store the offset
// of the sprite's position from the mouse. This is
// so we can position the sprite based on where the
// cursor is instead of the sprite's origin (the top-
// left corner) jumping to the mouse.
if offsetX = -1
offsetX = mousex() - sprite x(i)
offsetY = mousey() - sprite y(i)
endif
// Store selected sprite's ID
selectID = i
// Exit the loop early once a sprite collision is detected
exit
endif
next i
else // sprite is selected
// Update sprite's position
sprite selectID, mousex()-offsetX, mousey()-offsetY, selectID
endif
else
// Reset flags
offsetX = -1
selectID = 0
endif
// Display some helpful feedback
set cursor 0,0
print "Sprite Count: ",_ImgIdx
sync
UNTIL escapekey() = 1
end
////////////////////////////////////////////////////////////////
// Give an image, this will load the image and assign it
// to a sprite. Both image ID and sprite ID will be the same.
///////////////////////////////////////////////////////////////
function importSprite(imgPath$)
// increment our image pointer.
// this keeps track of how many sprites we have
inc _ImgIdx, 1
// load the image
load image imgPath$, _ImgIdx
// create the sprite and center its position on screen
sprite _ImgIdx, screen width()/2 - image width(_ImgIdx)/2, screen height()/2 - image height(_ImgIdx)/2, _ImgIdx
endfunction

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds