|
When creating games, you will often want to animate your sprites. In
KPL, to animate sprites, you use the ‘MoveSpriteToPoint’ command,
which we looked at earlier in conjunction with an iteration loop.
To make it look at though a sprite is moving smoothly across the screen,
we actually move the sprite by a few co-ordinates, stop the program
for a few milliseconds and then move the sprite again. By doing
this at a rate of approximately 30hz (30 movements every second), the
brain thinks the sprite is moving smoothly.
Setup a for loop, using the variable ‘Count’ with initial
value 0 and final value 400.
Inside the loop, move the a preloaded sprite to co-ordinate (Count,
Count) and set a delay of 5ms. Eg
Define
Count As Int
LoadSprite("SpaceShip", "UFO.gif")
ShowSprite("SpaceShip")
For Count
= 1 To 400
MoveSpriteToPoint("SpaceShip",
Count, Count)
Delay(5)
Next
Now, when you run your program, a UFO will move diagonally down
the screen! By using iteration and moving the sprite so that
a co-ordinate is the iterated variable, we have got our sprite to move. The
same could be down with a while loop.
By changing the delay, we can change the speed. By changing
the parameter of the for loop, we can change the starting and ending
positions.
Exercises
- Make a sprite ‘fly’ horizontally across the screen
- Make a sprite ‘fall’ straight down the screen, with
x co-ordinate 200.
Some sprites in KPL are made in ‘Graphics Interchange Format’ (GIF). This
means they are made up of more than one picture. For example,
in KPL, go to the list of sprites and double click on missile1.gif. You
will see that it says ‘Frame 1 of 6’
This means it has 6 ‘frames’ which make up the picture. If
you scroll the bar along, you will see the different frames and
notice that the missile appears to be spinning.
KPL has a built in procedure which will make a sprite repeatedly cycle
through all the different frames. First, you need to make a mental
note of how many frames there are on this screen. Next, go into
your code and use the following code, replacing ‘NumberOfFrames’ with
the relevant number:
Define Count
As Int Define Timeline
As Int[NumberOfFrames]
LoadSprite("MainSprite", "UFO.gif")
MoveSpriteToPoint("MainSprite",
20, 200)
ShowSprite("MainSprite")
For Count
= 1 To NumberOfFrames
Timeline[Count]
= 100
Next
SetSpriteAnimationTimeline("MainSprite",
True, Timeline)
In this example, we have loaded a sprite, along with a variable
of type Int[6] (the square brackets are important!). We have
then set the time which each frame should be displayed for to 100. We
used a for loop in the above code, because it is easier than copying
and pasting code. For example, if we had used Earth.gif, which
has 15 frames, we would have to copy and paste 15 times, which is silly.
Remember that the sprite will only animate whilst the program is running,
therefore in the above example, nothing would happen as the program
stops as soon as the animation starts. You can use the following
code to keep the sprite going.
While True=True
Delay(1)
End While
This works, because True is always going to equal True, therefore
the program never exists the loop, yet the loop doesn’t really
do anything, so you just see your sprite being animated.
Exercises
- Use a for loop to set the animation timeline for ‘Earth.gif’
- Use a for loop to set the animation timeline for ‘EnemyShip1.gif’
|