by Ernesto Quezada aka _Bruno : January 22 2006
In the previous page, I started
explaining the code for getting our hero Pawel to move. We will keep making
modifications to our code, but in this page, I will finish up my code
explanation.
Let's go through the code you used in the previous page:
- //---- variables ----
- var
steps:Number
= 5;
- var
spriteX:Number
= 265;
- var
spriteY:Number
= 265;
First we declare the variables, steps are the number of pixels our hero will move, spriteX and spriteY will help us to give value to the X and Y position of our hero.
The first function checks if we are pressing the keys that
are making our hero to do things:
- if (Key.isDown(Key.RIGHT)
&& spriteX<510)
{
- spriteX
+=
steps;
- knight.legs.play();
- }
The above code essentially says that only if we press the Right arrowKey and our hero is not outside of the stage, our hero will move to the right (remember, steps = 5 pixels)
and play the legs animation.
- else
if (Key.isDown(Key.LEFT)
&& spriteX>40)
{
- spriteX
-=
steps;
- knight.legs.play();
- }
This code is the same as above, except I now check for the Left arrow key and
ensure that our hero is not outside the left edge of our stage.
If we press the Up arrowKey on our keyboard, the animation in our arms movieclip will play.
- function
updatePawel()
{
- knight._x
=
spriteX;
- knight._y
=
spriteY;
- }
This function tells Flash that our hero will take the X and Y values of the variables spriteX and spriteY.
- this.onEnterFrame
=
function()
{
- checkKeys();
- updatePawel();
- };
This onEnterFrame event checks to see if we are pressing keys on the keyboard
or if our hero is moving.
Onwards to the next page!
|
page 2 of
4 |
|
|