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.
See SpriteCollisionMode for more information about modes.
FACTS:
* Sprite Collision is enabled by default.
* Sprites default to collision Mode 0 (Rectangle). See SpriteCollisionMode to change.
* See the SpriteHit or SpritesOverLap functions to detect collisions.
* Sprites Tutorial has a section on Sprite Collision.
Mini Tutorial:
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 ; ========================================== Cls RGB(0,0,255) GetImage 1,0,0,32,32 Cls RGB(0,255,00) GetImage 2,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=2 To 50 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 Cls RGB(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 ; Draw All of the sprites DrawAllSprites ; Draw the screen Sync ; Loop back to the DO statement Loop |
This example would output.
|