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:

Within our main loop:

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 )

Respond to input

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

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

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

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:

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

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

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.