Create a Flash Ball game with visual from above – Box2D version

After the old AS2 example and the Flare3D example published some days ago, it’s time to show you how to create a Flash Ball game with visual from above using Box2D.

Let’s jump directly to source code:

and let’s see the interesting lines:

Line 9: being a game with visual from above, there’s no gravity, that’s why the gravity vector in the constructor is set to a null vector.

Lines 24-33: this function, called at line 14, manages the debug draw. For more information about debug draw check Understanding Box2D debug draw.

Lines 34-45: this function simply adds a box to the world, as you have seen a million times. Just notice line 41 where isSensor property is set to true. If you don’t know what is a Box2D sensor, check Box2D Flash game creation tutorial – part 2.

Lines 46-61: this function will add the ball. Nothing new in this piece of code too, but note how I simulate air friction setting linearDamping property greater than 1 at line 52

Lines 94-95: according to the arrow key(s) being pressed, a different force is applied to the ball.

And here is the result:

Tap arrow keys to move the ball. Try to keep it inside the track. At the moment we don’t check if the ball is inside the track. We’ll do it in next step, when we’ll finish the prototype.

Download the source code.

  • Hi,

    Very nice tuts.. thanks :)

    I’m curios – Given the current setup with no gravity in the world – how would u simulate a “ball jump towards screen”?

  • Emanuele Feronato

    … using math…

  • Ochonma Victor

    Hello ferenato pls i really want to learn action script 3.0
    and how do i start because am in love with it but don’t really know
    to go about it…. just that am a very big fan of you in facebook.

  • Emanuele Feronato

    well, you can start with this blog and with active.tutsplus

    also, my book would be a best buy.