Create a Flash game like Rebuild Chile – Step 2: pushing debris

In this second part we’ll see how to push debris.

It’s not that hard. Let me explain when your bulldozer can move in one direction:

The bulldozer can move in a direction if:

1) It does not go outside the stage

2) Next tile in the selected direction is empty

3) Next tile in the selected direction has a boulder and:

3.a) The tile next to the boulder in the selected direction is empty

3.b) Moving the boulder in the selected direction does not push it outside the stage (you can’t push debris out of the stage at the moment).

This can be optimized if we surround the play area with solid, invisible tiles. This way we won’t have to check for bulldozer and stuff to go outside the stage and will become even more handy when we will introduce the capability to push debris outside the stage.

Let me show you the script:

And let’s see what changed:

Line 13: Now the map is bigger because it contains the surrounding unwalkable tiles, marked with 9. As you can see, draw_map function does not ever consider them when it’s time to draw the map. And we can skip all boundaries controls

Lines 56-71: Now the main function (it’s not properly the main one, but it’s the one I execute at every frame) does not move anything but calls walk function with two parameters: the horizontal and vertical offset. So walk(1,0) will mean I am going to walk to the right, walk(0,-1) means I am walking up, and so on.

And the walk function is the core of this script: it checks if the bulldozer can walk in the given direction (like I explained at the beginning of this post), moves (if possible) the bulldozer and moves (if possible) the boulder pushed by the bulldozer.

This is the tricky part. Look at line 33 how I assign a name to the debris movieclip. It’s an unique name combining “debris” word and its x and y position in the map array.

So, if I want to select the debris at position (3,4) I know its name will be debris_3_4. That’s how I select the debris movieclip to move at line 80. I know the position of the debris, so I know its name too, and I can easily select it with getChildByName function. Once I moved it, I change its name according to its new position, like I do at line 83.

And this is the result:

Arrow keys to move the bulldozer, try to push debris here and there.

No need to download, just replace the code in the first step with this one.

  • Quintus

    Hey,

    The bulldozer moves to fast. It’s hard for me to move 1 single step, cause then i make 2 or more steps.

    -Quintus

  • You should move bulldozer by setting setInterval, not on every frame.

  • Pingback: Emanuele Feronato - italian geek and PROgrammer()

  • Rennan

    Hii Emanuele!

    Today I was studying this prototype and I get one doubet. How did you´ve make to check external walls and bulldozer colisions?

    Regards