# Moving sprites

## Description

In this example we explore a very simple technique that allows us to gradually move a sprite from its initial location to the point where a user taps / clicks the screen.

## Overview

The process involved is as follows:

- load an image and create a sprite

Within our main loop:

- respond to user input
- calculate the destination point of our sprite
- turn on a flag to move our sprite
- move the sprite to its destination point

## Load an image and create a sprite

One image is going to be loaded and used for a sprite:

    image = LoadImage ( “lime.png” )
    sprite = CreateSprite ( image )

    image = agk::LoadImage ( “lime.png” );
    sprite = agk::CreateSprite ( image );

## Respond to input

When the user touches or clicks on the screen we will do the following:

- only respond if the sprite is not already moving to a location
- save the position of our sprite
- store the destination as the point where the user touched or clicked on the screen
- work out the distance between the original position and the destination
- set a flag that notifies our sprite it needs to move to the new location

Basic touch or click input is handled through the use of three commands:

- GetPointerPressed
- GetPointerX
- GetPointerY

The command GetPointerPressed returns a value of 1 whenever the user has touched or clicked the screen. When this happens you can use GetPointerX and GetPointerY to determine the location of the input.

Here’s the code for handling input and setting up a new location for the sprite:

    if GetPointerPressed ( ) = 1
        x = GetPointerX ( )
        y = GetPointerY ( )

        if ( move = 0 )
            originalX# = GetSpriteX ( sprite )
            originalY# = GetSpriteY ( sprite )

            destinationX# = x
            destinationY# = y
            distanceX# = destinationX# - originalX#
            distanceY# = destinationY# - originalY#
            distanceFromAtoB# = sqrt ( ( distanceX# * distanceX# ) + ( distanceY# * distanceY# ) )

            if ( distanceFromAtoB# <> 0.0 )
                directionX# = distanceX# / distanceFromAtoB#
                directionY# = distanceY# / distanceFromAtoB#
            endif
        endif
    endif

    if ( agk::GetPointerPressed ( ) == 1 )
    {
        x = agk::GetPointerX ( );
        y = agk::GetPointerY ( );

        if ( move = 0 )
        {
            originalX = agk::GetSpriteX ( sprite );
            originalY = agk::GetSpriteY ( sprite );

            destinationX = x;
            destinationY = y;
            distanceX = destinationX - originalX;
            distanceY = destinationY - originalY;
            distanceFromAtoB = sqrt ( ( distanceX * distanceX ) + ( distanceY * distanceY ) );

            if ( distanceFromAtoB != 0.0f )
            {
                directionX = distanceX / distanceFromAtoB;
                directionY = distanceY / distanceFromAtoB;
            }
        }
    }

## Move the sprite to its destination point

With the previous block of code we worked out a location for the sprite to move to and a flag was set to notify us of this change. Within the remainder of the main loop we need to check whether this flag is true, and if so update the position of our sprite, using the following process:

- see whether the move value is greater than 0
- determine where we need to move to based on the original location and the direction
- update the position of our sprite
- reset the move value once the sprite reaches its destination

    if ( move > 0 )
        newX# = originalX# + directionX# * move
        newY# = originalY# + directionY# * move

        if ( move < distanceFromAtoB# )
            move = move + 2
        else
            move = 0
        endif

        SetSpritePosition ( sprite, newX#, newY# )
    endif

    if ( move > 0 )
    {
        newX = originalX + directionX * move;
        newY = originalY + directionY * move;

        if ( move < distanceFromAtoB )
        {
            move = move + 2;
        }
        else
        {
            move = 0;
        }

        agk::SetSpritePosition ( sprite, newX, newY );
    }

The move variable is used as a way of saying how far we are along the line to our new destination. Initially it is 0, and after a user has touched / clicked the screen and the sprite starts moving, it gets incremented by 2 pixels each frame. Once it is greater than or equal to the distance value we know our sprite has reached its location.

## Full code listing

Everything is now in place. Here's the final code for our program:

    SetVirtualResolution ( 320, 480 )

    CreateSprite ( LoadImage ( "background4.jpg" ) )

    image = LoadImage ( “lime.png” )
    sprite = CreateSprite ( image )

    do
        Print ( "Touch or click the screen to move the" )
        Print ( "sprite to that location" )

        if GetPointerPressed ( ) = 1

            x = GetPointerX ( )
            y = GetPointerY ( )
            if ( move = 0 )
                move = 1

                originalX# = GetSpriteX ( sprite )
                originalY# = GetSpriteY ( sprite )

                destinationX# = x
                destinationY# = y
                distanceX# = destinationX# - originalX#
                distanceY# = destinationY# - originalY#
                distanceFromAtoB# = sqrt ( ( distanceX# * distanceX# ) + ( distanceY# * distanceY# ) )

                if ( distanceFromAtoB# <> 0.0 )
                    directionX# = distanceX# / distanceFromAtoB#
                    directionY# = distanceY# / distanceFromAtoB#
                endif
            endif
        endif

        if ( move > 0 )
            newX# = originalX# + directionX# * move
            newY# = originalY# + directionY# * move

            if ( move < distanceFromAtoB# )
                move = move + 2
            else
                move = 0
            endif

            SetSpritePosition ( sprite, newX#, newY# )
        endif

        Sync ( )
    loop

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

        agk::CreateSprite ( agk::LoadImage ( "background4.jpg" ) );

        image = agk::LoadImage ( “lime.png” );
        sprite = agk::CreateSprite ( image );
    }

    void app::Loop ( void )
    {
        agk::Print ( "Touch or click the screen to move the" );
        agk::Print ( "sprite to that location" );

        if ( agk::GetPointerPressed ( ) == 1 )
        {
            x = agk::GetPointerX ( );
            y = agk::GetPointerY ( );

            if ( move == 0 )
            {
                move = 1;

                originalX = agk::GetSpriteX ( sprite );
                originalY = agk::GetSpriteY ( sprite );

                destinationX = x;
                destinationY = y;
                distanceX = destinationX - originalX;
                distanceY = destinationY - originalY;
                distanceFromAtoB = sqrt ( ( distanceX * distanceX ) + ( distanceY * distanceY ) );

                if ( distanceFromAtoB != 0.0f )
                {
                    directionX = distanceX / distanceFromAtoB;
                    directionY = distanceY / distanceFromAtoB;
                }
            }
        }

        if ( move > 0 )
        {
            newX = originalX + directionX * move;
            newY = originalY + directionY * move;

            if ( move < distanceFromAtoB )
            {
                move = move + 2;
            }
            else
            {
                move = 0;
            }

            agk::SetSpritePosition ( sprite, newX, newY );
        }
    }

## Conclusion

This concludes the example of moving a sprite from one point to another. It’s a very simple technique that can be used for sprites in your games.