SpriteNumber = The Index of the sprite you wish to enable collision in CollisionFlag = The collision state (0= off, 1= on)
Returns: NONE
SpriteCollision enable/disables collision for this sprite. When activated (Set to True (1) ) this sprite can be checked for impacts against other sprites of the same collision class.
PlayBASIC has a very powerful Sprite Collision engine that includes various collision modes. Ranging from rectangle (static/rotated), circle, sliding, polygonal and of course Pixel Perfect.
This example is in 3 parts, part 1 creates two coloured images. Part 2 then creates both out test sprite (sprite #1) and a group of randomly position sprites to check collisions again. Part 3 is main loop, where it check for any collisions with sprite #1.
; ========================================== ; Part 1 - Create a pair of coloured images ; ==========================================
ClsRGB(0,0,255) GetImage1,0,0,32,32
ClsRGB(0,255,00) GetImage2,0,0,32,32
; ============================= ; Part 2a Create Users Test Sprite ; =============================
; Create Sprite 1, and assign it image 1 TestSprite=NewSprite(0,0,1) CenterSpriteHandle TestSprite
; Enable Collision for Sprite #1 SpriteCollision TestSprite,on
; Set Sprite #1 to collision Class %0001 SpriteCollisionClass TestSprite,%0001
; ============================= ; Part 2b Create some sprites to hit ; =============================
; Create a bunch fo sprites to check collision against For Sprites=2To50 x=Rnd(GetScreenWidth()-32) y=Rnd(GetScreenHeight()-32) Spr=NewSprite(x,y,2)
; Enable Collision for this sprite SpriteCollision Spr,on
; Set sprite to Collision Class %0010 SpriteCollisionClass Spr,%0010
Next
; ============================= ; Part 3- The Main Loop ; =============================
; Start a DO/Loop Do ; Clear the screen ClsRGB(10,20,30)
; Display a message Print "Checking for Sprite Collisions"
; Position our Test Sprite at the mouses position PositionSprite TestSprite,MouseX(),MouseY()
; Check if test sprite hits any sprite ; of this sprite class %0010 ThisSprite=SpriteHit(TestSprite,GetFirstSprite(),%0010)
; If there was impact, then can we loop through and ; find any other sprites it might have hit also While ThisSprite>0
Print "Hit Sprite:"+Str$(ThisSprite)
; Get the Sprite after the one test sprite just hit. NextSprite=GetNextSprite(ThisSprite)
; Check the remaining sprites against TestSprite. ThisSprite=SpriteHit(TestSprite,NextSprite,%0010) EndWhile