Testing your character movement (Part 4)

September 30, 2011
____________________________________________________________________

How to make the main character move?

If you take a look at Figure 10.1, you will see that you have a movie clip of you main character in scene 1. Left click at it once and open the actions script(by pressing F9 or choose Window --> Actions).

 
Figure 10.1

Put the code as show below and I will explain next.

onClipEvent (enterFrame) {
    this._x = /:move_x;
    this._y = /:move_y;
        if (Key.isDown(Key.RIGHT)) {
            this._xscale = 100;
            /:face = "right";
            /:status = "walk";
            /:move_x = /:move_x+10;
        } else if (Key.isDown(Key.LEFT)) {
            this._xscale = -100;
            /:face = "left";
            /:status = "walk";
            /:move_x = /:move_x-10;
        }
}

You can use the script Assist as shown in Figure 10.2.




Figure 10.2

onClipEvent (enterFrame) {

    This will perform actions when a particular movie clip event occurs. As you can see in the parenthesis "(enterFrame)", the "enterFrame" in the parenthesis is called "event". Whenever the timeline enters this frame, the code will be activated. There are many more events such as "Load', "Unload", "Mouse down", "Mouse up", ect. which I will not talk about them in details here.
_____________________________________________________________________

this._x = /:move_x;
this._y = /:move_y;

     These two lines are the position properties of your main character movie clip.

this._x means the value of the x position.
this._y means the value of the y position.

I set tthe first one (this._x) to be equal to my variable "/:move_x" which I declared this value at the first frame of the scene. Same thing for "this._y".

Let's just say that at the first frame, I set /:move_x = 200 and /:move_y = 100.


Figure 10.3

Then when the game runs. The position of the main character will be at x = 200, y = 100.

___________________________________________________________________

if (Key.isDown(Key.RIGHT)) {

   This command will detect what you do with the keyboard. If you press the right arrow key down, this code will be active.
___________________________________________________________________

this._xscale = 100;

   This line means the scale of you x direction is 100 % (the original x width scale).

___________________________________________________________________

/:face = "right";
/:status = "walk";


   These two lines are nothing but to set the variables(that you make it up) for the states of the main character that will be used for other actions. Like it said, /:face = "right" is just to make the value of /:face to be "right". And /:status = "walk", intuitively, is the value of /:status saying that now it's on the "walk" state.

____________________________________________________________________

/:move_x = /:move_x+10;

   The value of /:move_x is increasing by 10. This makes the main character move to the right by 10 pixels.

_____________________________________________________________________

if (Key.isDown(Key.LEFT)) {

   If you press the left arrow key down on your keyboard, this code will be active.
___________________________________________________________________

this._xscale = -100;

   This line means the scale of you x direction is 100 % (the original x width scale) but the minus sign makes it flip vertically, i.e. the character will be flipped to the other side.

___________________________________________________________________

/:face = "left";

   Like it said, /:face = "left" is just to make the value of /:face to be "left".
____________________________________________________________________

/:move_x = /:move_x - 10;


   The value of /:move_x is decreasing by 10. This makes the main character move to the left by 10 pixels.

_____________________________________________________________________

Yeah, now you can test your character movement (left and right only). I hope you are getting some idea how to control it. Good luck ;)