The following code example will create three sprite arrays of three sprites for each array. The code will select the sprite with the lowest sprite depth number (closer to top of the screen for all groups of over lapping sprites no matter which array they belong to. I have tried to keep it straightforward and included some comments. I hope it is helpful. It seems to work for me, but I am not sure exactly what you would like. I also included some code to swap the sprite depth of the top sprite with a sprite in the collision group that has a higher sprite depth number.
// Project: SelectlowDepthSprite
// Created: 2019-01-03
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "depthtest" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
Global mw as Integer = 1024 //mw and mh are used to keep sprites from being moved off screen
Global mh as Integer = 768
clr = MakeColor(255, 255, 255)
//Establish 3 arrays for maximum number of sprites in each array--Each array has 3 sprites in the array.
Global maxspr1 = 3 //maximum number of sprites in array 1--no sprite may have the same sprite number and sprites zero is not allowed and sprite 1 is the cursor sprite-so this array really starts with sprite number 2, so sprites included are sprites 2, 3, and 4.
Global maxspr2 = 3 //maximum number of sprites in array 2 is 3, but sprites 0 and 1 are not part of this group and sprites 2, 3, and 4 are part of array1, so this includes sprites 5, 6, and 7.
Global maxspr3 = 3 //Sprites included are 8, 9, and 10
Dim numspr1[maxspr1] //First array of sprites--Tracks number of colliding sprites by setting any sprites colliding at the screen location of sprite number one--if the sprite collides with sprite one then the item is set to 1 else it will be 0 for no collision with sprite number one
Global maxnumspr as Integer = 10 //setting variable to check all sprites. This is the maximum number of sprites, including sprite number 1.
Dim sprcoll[maxnumspr] //Tracks collision with other sprites for the first array of sprites
Dim numspr2[maxspr2] //second array of sprites to be tracked
Dim numspr3[maxspr3] //third array of sprites
//load data for arrays
sprcount = 2 //used to assign sprite numbers for each array
For sprid = 1 to maxspr1 //do not use 0 or 1(sprite 1 is used for following the cursor) in the array
numspr1[sprid] = sprcount
sprcoll[sprcount] = 0 //setting collision of sprites to 0, which at this point means it is not colliding--It is set to 1 when the sprite collides with another sprite in the array
sprcount = sprcount + 1
Next sprid
For sprid = 1 to maxspr2 //Used for following the cursor) or sprites 5, 6, and 7 in this array
numspr2[sprid] = sprcount
sprcoll[sprcount] = 0 //setting collision of sprites to 0, which at this point means it is not colliding--It is set to 1 when the sprite collides with another sprite in the array
sprcount = sprcount + 1
Next sprid
For sprid = 1 to maxspr3
numspr3[sprid] = sprcount //Used for following the cursor) or sprites 8, 9, and 10 in this array
sprcoll[sprcount] = 0 //setting collision of sprites to 0, which at this point means it is not colliding--It is set to 1 when the sprite collides with another sprite in the array
sprcount = sprcount + 1
Next sprid
Global sprnum as Integer = 0 //Tracks the sprite number first colliding with the cursor
Global ctspr as Integer = 0 //This tracks number of sprites that overlap at collision with sprite number one
Global lowspr as Integer = 12 //This tracks the sprite number of array1 with the lowest depth at the collision point--uses the number 6 as it is higher than the greatest depth for the sprites used
Global lowsprnum as Integer = 12 //Tracks the first (lowest depth--closest to screen) sprite in the collision for array1
CreateImageColor(1, 255, 255, 255, 0) //This sprite is the sprite used to determine if their are any sprites at the location of the cursor as this sprite follows the cursor
CreateSprite(1, 1)
SetSpriteSize(1, 1, 1)
SetSpritePosition(1, 0, 0)
SetSpriteDepth(1, 0)
//No sprites have the same depth
//First array of sprites--colored a shade of red
CreateImageColor(2, 255, 0, 0, 255)
CreateSprite(2, 2)
SetSpriteSize(2, 55, 55)
SetSpritePosition(2, 155, 155)
SetSpriteDepth(2, 1)
CreateImageColor(3, 250, 75, 75, 255)
CreateSprite(3, 3)
SetSpriteSize(3, 55, 55)
SetSpritePosition(3, 185, 155)
SetSpriteDepth(3, 2)
CreateImageColor(4, 200, 100, 100, 255)
CreateSprite(4, 4)
SetSpriteSize(4, 55, 55)
SetSpritePosition(4, 175, 185)
SetSpriteDepth(4, 3)
//Second array of sprites--colored a shade of green
CreateImageColor(5, 0, 255, 0, 255)
CreateSprite(5, 5)
SetSpriteSize(5, 55, 55)
SetSpritePosition(5, 320, 175)
SetSpriteDepth(5, 4)
CreateImageColor(6, 100, 250, 100, 255)
CreateSprite(6, 6)
SetSpriteSize(6, 55, 55)
SetSpritePosition(6, 300, 175)
SetSpriteDepth(6, 5)
CreateImageColor(7, 175, 200, 175, 255)
CreateSprite(7, 7)
SetSpriteSize(7, 55, 55)
SetSpritePosition(7, 300, 195)
SetSpriteDepth(7, 6)
//Third array of sprites--colored a shade of blue
CreateImageColor(8, 0, 0, 255, 255)
CreateSprite(8, 8)
SetSpriteSize(8, 55, 55)
SetSpritePosition(8, 500, 185)
SetSpriteDepth(8, 7)
CreateImageColor(9, 100, 100, 255, 255)
CreateSprite(9, 9)
SetSpriteSize(9, 55, 55)
SetSpritePosition(9, 515, 185)
SetSpriteDepth(9, 8)
CreateImageColor(10, 250, 0, 255, 255)
CreateSprite(10, 9)
SetSpriteSize(10, 55, 55)
SetSpritePosition(10, 525, 205)
SetSpriteDepth(10, 9)
Global Message1 as String = "" //"No sprite overlap at collision"
Global Message2 as String = ""
Global Message3 as String = "Sprite selected is: "
Global Message4 as String = "Press left mouse to move selected sprite."
Global Message5 as String = "Press right mouse to swap a lower sprite depth at cursor with a higher sprite depth."
CreateText(1, Message1)
SetTextSize(1, 30)
SetTextPosition(1, 10, 500)
CreateText(2, Message2)
SetTextSize(2, 30)
SetTextPosition(2, 10, 550)
CreateText(3, Message3)
SetTextSize(3, 30)
SetTextPosition(3, 10, 600)
CreateText(4, Message4)
SetTextSize(4, 30)
SetTextPosition(4, 10, 650)
CreateText(5, Message5)
SetTextSize(5, 30)
SetTextPosition(5, 10, 700)
do
For sprcheck = 2 to maxnumspr //use the total number of sprites to go through all sprites for all arrays. In this example the number is 9 (sprites 2 through 10)
If GetSpriteExists(sprcheck) = 1
If GetSpritecollision(sprcheck, 1) = 1 //finds first sprite colliding with cursor sprite number 1
sprcoll[sprcheck] = 1
ctspr = 1
sprnum = sprcheck // used to hold first sprite number colliding with the surcor
Exit //exits loop because it found a sprite colliding with the cursor sprite
Endif
Endif
Next sprcheck
If sprnum > 0 //Plays through loop if a sprite is colliding with the cursor to determine if any sprites colliding with that first sprite are also colliding with the cursor
For sprcheck2 = sprnum + 1 to maxnumspr //use max number of sprites to test for collision under cursor starting with sprite after the sprite detected colliding withe cursor
If GetSpriteExists(sprcheck2) = 1
If GetSpriteCollision(sprcheck2, 1) = 1 //check collision with cursor
sprcoll[sprcheck2] = 1 //set to 1 for collision with cursor
ctspr = ctspr + 1
Endif
Endif
Next sprcheck2
Endif
If ctspr >= 1 //determines which sprite is lowest depth
For sprid = 2 to maxnumspr
If GetspriteExists(sprid) = 1
If sprcoll[sprid] = 1 //Only checks sprites identified as colliding with cursor
If GetSpriteDepth(sprid) < lowsprnum //checks if sprite depth is a lower number than the current depth number
lowsprnum = GetSpriteDepth(sprid) //Assigns current sprite number to the lowest sprite depth number
lowspr = sprid //Assignes the current sprite as the sprite number with the lowest sprite depth
Endif
Endif
Endif
Next sprid
Endif
If GetSpriteExists(lowspr) = 1
If lowsprnum < maxnumspr + 1 Then DrawBox(getspritex(lowspr), getspritey(lowspr), getspritex(lowspr) + GetSpriteWidth(lowspr), getspritey(lowspr) + GetSpriteHeight(lowspr), clr, clr, clr, clr, 0)
Endif
If ctspr >= 2
Message2 = str(ctspr) + " sprites are overlapping at the cursor. "
Elseif ctspr = 1
Message2 = "No sprites are overlapping at the cursor. "
Elseif ctspr = 0
Message2 = "No sprites are selected. "
Endif
SetTextString(2, Message2)
//move sprite with cursor
oldmx = GetRawMouseX()
oldmy = GetRawMouseY()
While GetRawMouseLeftState() = 1 And GetSpriteExists(lowspr) = 1
Movesp(lowspr, oldmx, oldmy)
EndWhile
SetSpritePosition(1, GetPointerX(), GetPointerY()) //getrawmousex(), getrawmousey())
Print( ScreenFPS() )
Sync()
//Move a sprite to the foreground if more than one sprite is at the cursor
If GetRawMouseRightState() = 1 And GetSpriteExists(lowspr) = 1 And ctspr >= 2 Then Changedepth(lowsprnum, lowspr)
//Clears all tracking data
SetTextString(1, "")
SetTextString(3, "")
numbspr1 = 0
sprct=2 //starts the sprite number as 2 because sprite 1 is used at the cursor to determine the collisions.
For sprid = 1 to maxnumspr //do not use 0 or 1(sprite 1 is used for following the cursor) in the array
sprcoll[sprid] = 0 //setting collision of sprites to 0, which at this point means it is not colliding--It is set to 1 when the sprite collides with another sprite in the array
Next sprid
lowsprnum = maxnumspr + 1
lowspr = maxnumspr + 1
ctspr = 0
loop
Function Movesp(sprnum, omx1, omy1)
setposx = GetRawMouseX() //+ diffx
setposy = GetRawMouseY() //+ diffy
If setposy < 0 Then setposy = 0
mh = GetVirtualHeight()
If setposy > mh - 20 Then setposy = mh - 20
If setposx < 0 Then setposx = 0
mw = GetVirtualWidth()
If setposx > mw - 20 Then setposx = mw - 20
SetSpritePosition(sprnum, setposx ,setposy)
SetSpritePosition(1, setposx ,setposy)
Sync()
EndFunction
Function Changedepth(frontsprdepth, frontspr)
CreateEditBox(1)
SetEditBoxInputType( 1, 1 )
SetEditBoxPosition( 1, 10, 600 )
SetEditBoxMaxChars( 1, 22 )
SetEditBoxSize( 1, 200, 30 )
SetEditBoxTextSize( 1, 30 )
SetEditBoxFocus( 1, 1 )
If ctspr = 2
Message1 = "Choose a sprite to bring forward. The following sprite is available: "
Else
Message1 = "Choose a sprite to bring forward. The following sprites are available: " + Str(frontspr) + " " + Str(frontsprdepth)
Endif
convertsprnum = 0
sprselected = 0
Message3 = "Sprite selected is: "
SetEditBoxText( 1, Message3 )
For sprid = 2 to maxnumspr
If GetSpriteExists(sprid) = 1
If sprcoll[sprid] = 1 And sprid <> frontspr
Message1 = Message1 + Str(sprid) + " "
Endif
Endif
Next sprid
SetTextString(1, Message1)
enterstr$ = ""
While GetRawKeyPressed( 13 ) = 0 //return key pressed
enterstr$ = GetEditBoxText(1)
If Len(enterstr$) < 20
SetEditBoxText(1, Message3)
Endif
If Len(enterstr$)=21 Then convertsprnum = Val(Right(GetEditBoxText(1),1))
If Len(enterstr$)=22 Then convertsprnum = Val(Right(GetEditBoxText(1),2))
Sync()
EndWhile
If convertsprnum > 0
SetSpriteDepth(frontspr, GetSpriteDepth(convertsprnum))
SetSpriteDepth(convertsprnum, frontsprdepth)
Endif
convertsprnum = 0
DeleteEditBox(1)
Endfunction