Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 6 comments

  1. ViZgl

    on October 15, 2012 at 6:18 pm

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

  2. Create a Flash game like Roly Poly with Box2D – Step 2 – Emanuele Feronato « eaflash

    on October 16, 2012 at 9:20 am

    [...] on http://www.emanueleferonato.com Share this:TwitterFacebookLike this:LikeBe the first to like [...]

  3. Bhuwan

    on October 16, 2012 at 8:32 pm

    Hi!
    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)
    Thanks

  4. Bhuwan

    on October 16, 2012 at 8:45 pm

    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.
    Thanks.

  5. MC

    on October 19, 2012 at 3:08 am

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

  6. CHINVA

    on April 20, 2013 at 5:07 pm

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