All sprites have a group property that can be used for filtering collisions, which by default is set to be 0. Altering this might be useful in some situations, for example, your game may have blue and red robots running round a level, with the objective being to click or touch the blue robots and avoid the red robots. In this example the blue robots could be assigned a group of 1, with the red robots being assigned to group 2. All that's left is to make a call to the command GetSpriteHitGroup and pass in the ID of the group and the location on screen and then you can easily determine whether the user has hit the blue or red robots.
This example program creates 4 sprites, positions them at random locations on the screen and then assigns them to group 1. Another 4 sprites are created, positioned at random locations and then get assigned to group 2. Within the main loop GetSpriteHitGroup is used to determine when a user touches or clicks a sprite in group 2.
The code performs the following tasks:
SetVirtualResolution ( 320, 480 )
LoadImage ( 1, "chip5.png" ) LoadImage ( 2, "chip25.png" )
for i = 1 to 4 sprite = CreateSprite ( 1 )
SetSpritePosition ( sprite, Random ( 10, 280 ), Random ( 50, 400 ) ) SetSpritePhysicsOn ( sprite, 1 ) SetSpriteGroup ( sprite, 1 ) next i
for i = 1 to 4 sprite = CreateSprite ( 2 )
SetSpritePosition ( sprite, Random ( 10, 280 ), Random ( 50, 400 ) ) SetSpritePhysicsOn ( sprite, 1 ) SetSpriteGroup ( sprite, 2 ) next i
Notice how physics is turned on for the sprites prior to setting a group. The reason for this is that in order to use groups a sprite must have physics enabled, so in our example the sprites have physics turned on and are set to be static objects.
Within our main loop an if statement is used to determine when the user hits or touches the screen, at this point GetSpriteHitGroup is called. The command takes three parameters - a group ID and an X and Y location, which for our purposes will be the group 2 and the location of the input. The return value is an ID number for whatever sprite was hit, this gets stored in a variable that is printed out on screen:
do Print ( "Group 2 sprite ID:" ) Print ( sprite )
if ( GetPointerPressed ( ) = 1 ) sprite = GetSpriteHitGroup ( 2, GetPointerX ( ), GetPointerY ( ) ) endif
Sync ( ) loop
Whenever the user touches or clicks the screen only sprites from group 2 register input, the sprites in group 1 are ignored. When the user hits empty space the command GetSpriteHitGroup returns a value of 0.
Everything is now in place. Here's the final code for our program:
SetVirtualResolution ( 320, 480 )
backdrop = CreateSprite ( LoadImage ( "background2.jpg" ) ) SetSpriteColorAlpha ( backdrop, 100 )
LoadImage ( 1, "chip5.png" ) LoadImage ( 2, "chip25.png" )
for i = 1 to 4 sprite = CreateSprite ( 1 )
SetSpritePosition ( sprite, Random ( 10, 280 ), Random ( 50, 400 ) ) SetSpritePhysicsOn ( sprite, 1 ) SetSpriteGroup ( sprite, 1 ) next i
for i = 1 to 4 sprite = CreateSprite ( 2 )
SetSpritePosition ( sprite, Random ( 10, 280 ), Random ( 50, 400 ) ) SetSpritePhysicsOn ( sprite, 1 ) SetSpriteGroup ( sprite, 2 ) next i
do Print ( "Group 2 sprite ID:" ) Print ( sprite )
if ( GetPointerPressed ( ) = 1 ) sprite = GetSpriteHitGroup ( 2, GetPointerX ( ), GetPointerY ( ) ) endif
Sync ( ) loop
By assigning sprites to groups it is possible to have extra control when interacting with them. As you can see from this example it's a very useful feature, and being able to quickly determine whether a user has touched a sprite with one command call can save a lot of time and allows you to focus on the more important elements of your game.