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.
The process involved is as follows:
Within our main loop:
One image is going to be loaded and used for a sprite:
image = LoadImage ( "lime.png" ) sprite = CreateSprite ( image )
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
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.
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
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.