if I understand correctly you're trying to test if one of many sprites on the screen has been hit at a certain x,y coordinate and then return the index of the sprite, but you want it to be returned as the global variable mySprite, and you want the return value to only be the sprite that is visible.
Depending on just how many sprites you're talking about, you could do it like this...
Use an array to hold the sprite indexes as you create them.
EDIT: Took image assignment out of the array loop as I doubt your using a seperate image for each sprite.
EDIT: fixed the name of the array in the dim statement, forgot the s on the end of the name.
Type _ScreenObject
Index as Integer
EndType
dim mySprites(NumberOfSprites) as _ScreenObject
mySpritesImage = LoadImage("test.png")
for i = 1 to NumberOfSprites
mySprites[i].Index = CreateSprite(mySpritesImage)
next i
Global mySprite as Integer
Now since GetSpriteHit does not take visiblity into account, you would need to check for the hit in another way...
if they are physics sprites you could use a 1 pixel wide by 1 pixel high dummy sprite and GetPhysicsCollision.
However if the sprites are not physics sprites you would need to use a 1 pixel wide/high invisible regular sprite and GetSpriteCollision
For a physics based sprite you could create the dummy sprite and test in your loop like this.
DeviceWidth# = GetDeviceWidth()
DeviceHeight# = GetDeviceHeight()
OnePixelWide# = 1 / DeviceWidth#
OnePixelHigh# = 1 / DeviceHeight#
TestSprite = CreateDummySprite()
SetSpriteSize(TestSprite,OnePixelWide#,OnePixelHigh#)
do
SetSpritePosition(TestSprite,XToTest,YToTest)
for i = 1 to NumberOfSprites
if GetPhysicsCollision(TestSprite,mySprites[i].Index) = 1 and GetSpriteVisible(mySprites[i].Index) = 1
mySprite = mySprites[i].Index
exit
endif
next i
loop
For non physics based sprites you could do it like this.
DeviceWidth# = GetDeviceWidth()
DeviceHeight# = GetDeviceHeight()
OnePixelWide# = 1 / DeviceWidth#
OnePixelHigh# = 1 / DeviceHeight#
TestSprite = CreateSprite(AnyImageYouWant)
SetSpriteSize(TestSprite,OnePixelWide#,OnePixelHigh#)
SetSpriteVisible(TestSprite,0)
do
SetSpritePosition(TestSprite,XToTest,YToTest)
for i = 1 to NumberOfSprites
if GetSpriteCollision(TestSprite,mySprites[i].Index) = 1 and GetSpriteVisible(mySprites[i].Index) = 1
mySprite = mySprites[i].Index
exit
endif
next i
loop
That will cycle through your sprites until it finds the first one that has been hit at the specified x,y coordinates, that is also visible, and then assign its index to the global variable mySprite and exit the for loop.
Is that what you're looking for?
Good luck...