Book Image

Box2D for Flash Games

Book Image

Box2D for Flash Games

Overview of this book

Physics games are getting more and more popular, and Box2D is the best choice if you are looking for a free, stable and robust library to handle physics. With Box2D you can create every kind of 2D physics game, only coding is not the fun part, but the game itself. "Box2D for Flash Games" will guide you through the process of making a Flash physics game starting from the bare bones and taking you by hand through complex features such as forces, joints and motors. As you are learning, your game will have more and more features, like the physics games you are used to playing. The book analyzes two of the most played physics games, and breaks them down to allow readers to build them from scratch in a step-by-step approach. By the end of the book, you will learn how to create basic primitive bodies as well as complex, compound bodies. Motors will give life to cars, catapults and siege machines firing bullets, while a complete collision management will make your game look even more realistic. If you want to make full Flash games with physics, then Box2D for Flash Games will guide you through the entire process of making a Flash physics game.
Table of Contents (15 chapters)
Box2D for Flash Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

If you look at the most successful Flash games, most of them use physics to add realism and features, which would not have been possible otherwise. This book will guide you through the creation of physics games using Box2D, a free open source physics engine, which is the most used one among game developers. Throughout the book, you will learn how to use Box2D while you create real games.

What this book covers

Chapter 1, Hello Box2D World, introduces what is Box2D, what you can do with it, how to include it in your Flash projects, and your first Box2D world.

Chapter 2, Adding Bodies to the World, explains how to add primitive and complex bodies to the world, and see them displayed on the stage. It also introduces units of measurement.

Chapter 3, Interacting with Bodies, looks at showing how to interact with bodies, selecting them with the mouse, and knowing their position in the world.

Chapter 4, Applying Forces to Bodies, explains how forces and impulses make bodies move in the Box2D world, and how to apply them to animate the world.

Chapter 5, Handling Collisions, answers questions such as: did two bodies collide, was it a hard collision, and where did they collide. We will also discover how to handle collisions among bodies.

Chapter 6, Joints and Motors, explains how to create complex structures using joints, and give them a life applying motors.

Chapter 7, Skinning the Game, explains how to render Box2D world using your own graphic assets.

Chapter 8, Bullets and Sensors, discusses the need for bodies with special attributes. It also explains how to use bullets and sensors for a more accurate simulation and for silent collisions.

What you need for this book

Flash CS5 or above is needed. You can download a fully functional free trial of Adobe Flash Professional CS6 from www.adobe.com/go/tryflash/.

Who this book is for

Do you already know AS3 basics and want to add to your games a great twist, thanks to physics? Then this book is for you, even if you don't know physics.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Nothing new, we just added an ENTER_FRAME event, but we needed it in order to run the simulation inside the updateWorld function."

A block of code is set as follows:

package  {
  import flash.display.Sprite
  import Box2D.Dynamics.*;
  import Box2D.Collision.*;
  import Box2D.Collision.Shapes.*;
  import Box2D.Common.Math.*;
  public class Main extends Sprite {
    public function Main() {
      trace("my awesome game starts here");
    }
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

package {
  import flash.display.Sprite;
  import flash.events.Event;
  import Box2D.Dynamics.*;
  import Box2D.Collision.*;
  import Box2D.Collision.Shapes.*;
  import Box2D.Common.Math.*;
  public class Main extends Sprite {
    public function Main() {
      var gravity:b2Vec2=new b2Vec2(0,9.81);
      var sleep:Boolean=true;
      var world:b2World = new b2World(gravity,sleep);
      addEventListener(Event.ENTER_FRAME,updateWorld);
    }
    private function updateWorld(e:Event):void {
      trace("my awesome simulation runs here");
    }
  }
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Please notice the names under AS Linkage as I will be using them in the code."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.