Sprite visibility

Description

After a sprite has been created, by default it is visible. To control the visible state of a sprite use the command SetSpriteVisible as demonstrated in this example.

Creating a sprite

This block of code creates a sprite using a penguin image:

LoadImage ( 1, "penguin.png" )
CreateSprite ( 1, 1 )
SetSpritePosition ( 1, 100, 300 )

Controlling visibility

The command SetSpriteVisible is used to control whether a sprite is visible or invisible, it takes two parameters:

Therefore, to hide our penguin sprite it's a case of calling SetSpriteVisible, setting the first parameter to 1 (ID of the sprite) and its visible state to 0:

SetSpriteVisible ( 1, 0 )

To retrieve the current visible state of a sprite call the command GetSpriteVisible and pass in the ID of the sprite to check:

visible = GetSpriteVisible ( 1 )

If the return value is 1 then the sprite is visible. When the return value is 0 the sprite is invisible.

Full code listing

Within the main loop, whenever the user clicks or touches the screen the visibility of the sprite is swapped:

SetVirtualResolution ( 320, 480 )

CreateSprite ( LoadImage ( "background3.jpg" ) )
LoadImage ( 1, "penguin.png" ) CreateSprite ( 1, 1 ) SetSpritePosition ( 1, 100, 300 )
do Print ( "Click or touch the screen to" ) Print ( "hide or show the penguin" )
if ( GetPointerPressed ( ) = 1 ) if ( state = 0 ) SetSpriteVisible ( 1, 0 ) state = 1 else SetSpriteVisible ( 1, 1 ) state = 0 endif endif
Sync ( ) loop

Conclusion

Being able to set the visibility of sprites is useful for many in game situations and hiding sprites will also boost performance.