Create a Flash game like Roly Poly with Box2D – Step 2

Welcome to the second step. It’s time to build a level and let the player rotate it with the mouse.

I am going to show you two ways to rotate the level, anyway let’s start fromt the beginning: in the making of step 1 I told you it’s a tile based game, so I am going to use an array to store the tiles.

At the moment each array item can have three values:

0: empty space
1: tile
2: the player

So let me write some more code:

There’s nothing interesting in it, apart from level array used to build the level.

This is what we have at the moment:

A static level with a dynamic sphere. We must first make the level dynamic, with a revolute joint to pin it to the stage, following the same concept explained int he post creation of a rotating room with Flash and Box2D, so I would change the script this way:

I used a revolute joint to pin the level to the stage, and made it dynamic. The problem is the level isn’t perfectly balanced, so it will start to rotate although there isn’t any player interaction:

We need a motor with an high torque to keep the level fixed in its starting position, no matter if it’s perfectly balanced, this way:

And now we have our dynamic pinned level pacificly waiting for our interaction:

Now, a simple mouse joint will let us have our first working example of a roly poly level:

And that’s it:

Click and drag the mouse everywhere to rotate the level.

Since I said I was showing you two ways to rotate the level, what about rotating it according to horizontal mouse position? The more the mouse on the left, the faster the rotation counter-clockwise, the more the mouse on the right, the faster the rotation clockwise, only acting on revolute joint motor speed.

And this is the result:

Move the mouse along its x axis to see the level rotate.

No need to download anything, just copy/paste the code you need into Main class you can find at step 1.

  • The simplest way is change gravity vector and rotate graphics based on it.

  • Pingback: Create a Flash game like Roly Poly with Box2D – Step 2 – Emanuele Feronato « eaflash()

  • Bhuwan

    Can you please tell where should I prefer WCK? As I think in the above game WCK would surely save a lot of time and effort?
    Also I am unable to find advanced tutorials on WCK? Can you please guide me on how should I proceed? I read the basic tutorial on this blog and have gone through the demo files it gives(though didn’t get some of them)

  • Bhuwan

    Also it would be very helpful if you can please explain the relative significance of wck, quickbox2D, citrus engine and other libraries/engines you have discussed on your blog.

  • MC

    you should say which lines were modified in the code on each step


    HI Emanuele
    can you put an example with texture or make a tutorial on how to texture a tiles made by box2d thanks

  • carolina

    Hello, you could make an example where obstacles are implemented?


  • carolina

    Hello, can you make Roly Poly with Box2D in phaser?