The SpritesOverlap function checks if two sprites have collided. If a collision occurs the function returns a True value (1), if not, it returns False(0)
FACTS:
* Sprite collisions occur when the collision regions of any two sprites overlap. See SpriteCollisionMode for the various collision modes.
* Also see CompareSpritePixels, SpriteHit, SpriteCollision
Mini Tutorial:
This example is in 3 parts,
Part 1 creates two coloured images.
Part 2 then creates both our test sprite (sprite #1) and a group of randomly positioned sprites to check collisions again.
Part 3 is the main loop, where it manually checks any collisions with sprite #1.
; ======================== ; Part 1 - Create an image ; ======================== Cls RGB(0,0,255) GetImage 1,0,0,32,32 Cls RGB(0,255,00) GetImage 2,0,0,32,32 ; ============================= ; Part 2- Create some sprites ; ============================= ; Create Sprite 1, and assign it image 1 CreateSprite 1 SpriteImage 1,1 CenterSpriteHandle 1 ; Enable Collision for Sprite #1 SpriteCollision 1,on ; Set Sprite #1 to collision Class %0001 SpriteCollisionClass 1,%0001 ; ===================================================== ; Create a bunch fo sprites to check collision against ; ===================================================== For Sprites=2 To 50 CreateSprite Sprites SpriteImage Sprites,2 x=Rnd(GetScreenWidth()-32) y=Rnd(GetScreenHeight()-32) PositionSprite sprites,x,y ; Enable Collision for this sprite SpriteCollision Sprites,on ; Set sprite to Collision Class %0010 SpriteCollisionClass Sprites,%0010 Next ; ============================= ; Part 3- The Main Loop ; ============================= ; Start a DO/Loop Do ; Clear the screen Cls RGB(0,0,0) ; Display a message Print "Manual Sprite Collisions" ; Position the Sprite 1 at the mouses position PositionSprite 1,MouseX(),MouseY() ; Manually chek sprite #1 against the other sprites ThisSprite=SpriteHit(1,GetFirstSprite(),%0010) For Spr=2 To 50 ; Check if Sprite 1 hits this sprite If SpritesOverlap(1,Spr) Print "Hit Sprite:"+Str$(Spr) EndIf Next ; Draw All of the sprites DrawAllSprites ; Draw the screen Sync ; Loop back to the DO statement Loop |
This example would output.
|