# Hitting a sprite

When checking whether a user has hit or clicked on a sprite there are two options:

- Call GetSpriteHitTest, which allows you to check whether a given point lies within the boundaries of a specific sprite.
- Use GetSpriteHit, which returns the ID number of a sprite that is under the specified point.

This example takes a closer look at the second option: GetSpriteHit. This command is particularly useful for a situation where you quickly want to find what sprite a user has touched or clicked on. Internally this command will check all sprites within the area and return the appropriate ID number. It's a great time saver and means you don't need to be cycling through and checking specific sprites.

Two sprites will be placed side by side and when the user clicks or hits them the ID number of that sprite will appear on screen.

## Getting started

The initial set up code sets a virtual resolution of 320 x 480, this is followed by loading the images "chip5.png" and "chip25.png".

A sprite on the left uses "chip5.png", while a sprite on the right uses "chip25.png":

    SetVirtualResolution ( 320, 480 )

    LoadImage ( 1, "chip5.png" )
    LoadImage ( 2, "chip25.png" )

    CreateSprite ( 1, 1 )
    CreateSprite ( 2, 2 )

    SetSpritePosition ( 1, 100, 50 )
    SetSpritePosition ( 2, 200, 50 )

    void app::Begin ( void )
    {
        agk::SetVirtualResolution ( 320, 480 );

        agk::LoadImage ( 1, "chip5.png" );
        agk::LoadImage ( 2, "chip25.png" );

        agk::CreateSprite ( 1, 1 );
        agk::CreateSprite ( 2, 2 );

        agk::SetSpritePosition ( 1, 100, 50 );
        agk::SetSpritePosition ( 2, 200, 50 );
    }

## Hitting a sprite

Within our main loop an if statement is used to determine when the user hits or touches the screen, at this point GetSpriteHit is called. The command takes two parameters - an X and Y location, which for our purposes will be 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 ( "Clicked on sprite" )
        Print ( sprite )

        if ( GetPointerPressed ( ) = 1 )
            sprite = GetSpriteHit ( GetPointerX ( ), GetPointerY ( ) )
        endif

        Sync ( )
    loop

    void app::Loop ( void )
    {
        agk::Print ( "Clicked on sprite" );
        agk::Print ( iSprite );

        if ( agk::GetPointerPressed ( ) )
            iSprite = agk::GetSpriteHit ( agk::GetPointerX ( ), agk::GetPointerY ( ) );

        agk::Sync ( );
    }

Whenever a user clicks or touches the screen the ID number of whatever sprite they were hitting will be displayed on screen. If no collision occurred then a value of 0 is returned from the command GetSpriteHit.

## Full code listing

The final code listing is:

    SetVirtualResolution ( 320, 480 )

    backdrop = CreateSprite ( LoadImage ( "background4.jpg" ) )
    SetSpriteColorAlpha ( backdrop, 200 )

    LoadImage ( 1, "chip5.png" )
    LoadImage ( 2, "chip25.png" )

    CreateSprite ( 1, 1 )
    CreateSprite ( 2, 2 )

    SetSpritePosition ( 1, 100, 50 )
    SetSpritePosition ( 2, 200, 50 )

    do
        Print ( "Clicked on sprite" )
        Print ( sprite )

        if ( GetPointerPressed ( ) = 1 )
            sprite = GetSpriteHit ( GetPointerX ( ), GetPointerY ( ) )
        endif

        Sync ( )
    loop

    void app::Begin ( void )
    {
        agk::SetVirtualResolution ( 320, 480 );

        int backdrop = agk::CreateSprite ( agk::LoadImage ( "background4.jpg" ) );
        agk::SetSpriteColorAlpha ( backdrop, 200 );

        agk::LoadImage ( 1, "chip5.png" );
        agk::LoadImage ( 2, "chip25.png" );

        agk::CreateSprite ( 1, 1 );
        agk::CreateSprite ( 2, 2 );

        agk::SetSpritePosition ( 1, 100, 50 );
        agk::SetSpritePosition ( 2, 200, 50 );
    }

    void app::Loop ( void )
    {
        agk::Print ( "Clicked on sprite" );
        agk::Print ( iSprite );

        if ( agk::GetPointerPressed ( ) )
            iSprite = agk::GetSpriteHit ( agk::GetPointerX ( ), agk::GetPointerY ( ) );

        agk::Sync ( );
    }

## Conclusion

The command GetSpriteHit is particularly useful for scenarios when numerous sprites are on screen. If you had a screen with 20 buttons on and wanted to know which one had been clicked, then all it will take is one call to GetSpriteHit. This is much more efficient than running through a loop and checking sprites individually.