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

Following a body with the camera in Box2D – The smart way

We are about to create an intelligent camera that will follow a body in Box2D. What’s the difference between an intelligent camera and a dumb one?

Simple… the intelligent camera follows the object keeping it in the middle of the screen as long as there are enough game area to fill the screen. The dumb one just keeps the object in the middle of the screen, sometime causing to have just a little part of the game on the screen.

Let me make things clearer with a real world example:

In this map, taken from Crystal Kingdom Dizzy, the cherry is followed by a smart camera, because the cherry is centered and the game area fills the entire screen.

Now look at this:

The cherry moved up and the camera followed it in a dumb way: the camera wanted to keep the cherry in the middle of the screen, but doing it will make the game scroll down even if there aren’t enough game area to fill the entire screen.

An intelligent camera should show the cherry this way:

So when the cherry moves in a direction that would make the game scroll away from the screen, the camera has to release it and don’t keep it in the middle of the screen until there is enough gaming area to fill the entire screen.

How do we apply this concept to the catapult prototype?

This way:

And this is the result:

Left and right arrows to move the catapult, spacebar to fire

Download the example and wait for the fully commented source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (19 votes, average: 4.37 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 9 comments

  1. Bwakathaboom

    on May 4, 2010 at 6:19 pm

    Off topic – do you usually use a single-class when developing your games? When do you decide to break off into multiple classes?

    I find other people’s coding styles fascinating.

  2. lala

    on May 5, 2010 at 11:51 am

    very off topic – i wasn’t able to find your contact information. i have been following this blog for almost a year. previously related to 3d graphics now going to learn flash scripting. your blog has always been amazing / informational / rapid update resouce. now when i wanted to start doing exercises, i found it extremely hard to find what i have read at some point. e.g: flash game structure discussions, box2d tutorials, if somehow you can improve menu / navigation. catagorise, then it will be amazing.
    specially if series of tuorials can come under a menu.
    sorry if this disturbs or annoyed.

  3. Lorenzo

    on May 5, 2010 at 4:28 pm

    altro off topic: in questi giorni continuo a ricevere errore 500 quando tento di accedere al tuo sito.

  4. AlexRath

    on May 9, 2010 at 8:42 pm

    Hey,

    how about some Ease?
    An elastic following, not so hard…
    You know, softer, so that the Ball is not always exact in the middle.
    That shows the speed better.

  5. Labici Danut

    on May 10, 2010 at 2:35 pm

    Hmmm abit hard for me but I’ll try and see what happen

  6. Peter

    on June 11, 2010 at 3:29 pm

    I just wanted to say thanks. Your tutorials have been more help than all the other Box2D resources I’ve found put together. Keep up the good work!

  7. Csomakk

    on August 30, 2010 at 9:33 pm

    hello Emanuele!
    I want to ask you if there is a way to zoom in or out in a box2d application?
    thanks

  8. ronaldmac

    on September 29, 2010 at 2:56 pm

    whats stage,x,y?where is it declared?

  9. Following a body with the camera in Box2D

    on May 25, 2012 at 3:40 am

    [...] http://www.emanueleferonato.com/2010/05/04/following-a-body-with-the-camera-in-box2d-the-smart-way/ [...]