Create a Flash game like Rebuild Chile – Step 3: smooth movement

In this 3rd part I’ll cover a feature requested by some readers… smooth movement.

In step 1 and 2, the character and the debris move square by square.

This leads to a unplayable game, because the bulldozer moves too quickly.

We need a smoother movement.

Here is how will it work:

The bulldozer now has two states: moving and not moving

When the bulldozer is not moving, it simply waits for the player to press a key, like in previous steps.

When the bulldozer is moving, the player cannot interact with it (basically the player can’t stop it when is moving from one tile to another) and the bulldozer (and eventually the debris the bulldozer is pushing) moves by a certain amount of pixels, in this case just 10 pixels, until it completely moves to next tile. Then the bulldozer returns to a not moving state.

The interesting thing is we know the maximum number of moving objects is two: the bulldozer and eventually the debris.

This is the script:

Let’s see the new code

Line 12: variable to store whether the bulldozer is moving or not

Lines 13-14: variables to store bulldozer’s x and y direction

Line 15: name of the debris we have to move (if any)

Line 61: as said, I check for user keyboard input only if the bulldozer is not moving

Line 77: if the bulldozer is moving… (notice there isn’t an else to process the movement in the same frame the user pressed a key)

Lines 78-79: moving the bulldozer by 10 pixels (1/5 a tile) in the required direction

Lines 80-85: same thing with the debris, if I have to move it

Lines 86-94: when the bulldozer is on the tile, stop moving it, update the map and the debris name.

and this is the result:

first step with this one.

  • Wow !

    It is really smooth.

    Nice hack ^^

  • Brad

    Awesome! Thanks so much for writing these tutorials, appreciate it!