Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Box2D for Flash Games

You're reading from  Box2D for Flash Games

Product type Book
Published in Nov 2012
Publisher Packt
ISBN-13 9781849519625
Pages 166 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Box2D for Flash Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Hello Box2D World 2. Adding Bodies to the World 3. Interacting with Bodies 4. Applying Forces to Bodies 5. Handling Collisions 6. Joints and Motors 7. Skinning the Game 8. Bullets and Sensors Index

Chapter 3. Interacting with Bodies

Every Box2D based game has its own way to interact with bodies. Totem Destroyer and Red Remover allow the player to destroy bodies by clicking them with the mouse, while Angry Birds makes bodies (birds) fly by dragging them. You already know how to create primitive and complex bodies; it's time to see how Box2D allows us to interact with bodies in its world.

In this chapter you will learn various ways to interact with and get information from Box2D bodies, including:

  • Selecting bodies with the mouse

  • Destroying bodies

  • Setting custom properties to bodies

  • Looping through all bodies in the world

  • Getting body information

By the end of the chapter, you will have a completely playable Totem Destroyer level.

The simplest and most intuitive way by which we can interact with Box2D bodies is destroying them with a mouse click.

Selecting and destroying bodies with a mouse click


We need to complete the Totem Destroyer prototype, so these concepts will be applied to the script you made in Chapter 2, Adding Bodies to the World. You should have a totem ready to be destroyed with an idol on top of it.

  1. Before we see how to select and destroy bodies, we need to add a mouse click listener, so we need to import a new class to handle mouse events in our Main class:

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import Box2D.Dynamics.*;
    import Box2D.Collision.*;
    import Box2D.Collision.Shapes.*;
    import Box2D.Common.Math.*;
  2. Then we can add the mouse listener in the Main function:

    public function Main() {
      world=new b2World(new b2Vec2(0,5),true);
      debugDraw();
      brick(275,435,30,30);
      brick(365,435,30,30);
      brick(320,405,120,30);
      brick(320,375,60,30);
      brick(305,345,90,30);
      brick(320,300,120,60);
      idol(320,242);
      floor();
      addEventListener(Event.ENTER_FRAME,updateWorld);
      stage...

Assigning custom attributes to bodies


Custom attributes can be of any type, but at the moment we'll just add a string: breakable for breakable bricks, and unbreakable for unbreakable bricks.

  1. First, we'll be passing the string as an argument of the brick function, so to reproduce the first level of Totem Destroyer we'll modify the Main function in the following way:

    public function Main() {
      world=new b2World(new b2Vec2(0,5),true);
      debugDraw();
      brick(275,435,30,30,"breakable");
      brick(365,435,30,30,"breakable");
      brick(320,405,120,30,"breakable");
      brick(320,375,60,30,"unbreakable");
      brick(305,345,90,30,"breakable");
      brick(320,300,120,60,"unbreakable");
      idol(320,242);
      floor();
      addEventListener(Event.ENTER_FRAME,updateWorld);
      stage.addEventListener(MouseEvent.CLICK,destroyBrick);
    }
  2. And now let's have a look at how the brick function changes:

    private function
    brick(pX:int,pY:int,w:Number,h:Number,s:String):void {
      var bodyDef:b2BodyDef=new b2BodyDef();
      bodyDef.position.Set...

Looping through bodies and getting their properties


The next example will build some kind of information display that monitors the idol. At every frame, we'll read the idol position, rotation, and speed. This way, you can assign events according to idol properties, such as giving a bonus if the idol did not fall down, or if it never reached a certain y-speed, or if it moved too far on the left, and so on. Knowing body properties is also very useful when you want to skin your game, as you can synchronize custom graphic assets to what happens in the Box2D World.

One step at time, let's start with our idol monitor. We are going to display idol data in a dynamic text field, so we need to make some basic changes to our class. I won't explain such changes as they are simple AS3 routines you should already know.

  1. First, we import the required classes to dynamically generate a text field and give it some style:

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import...

Summary


Now you know how to select and destroy a body, as well as get body information. With these concepts you can build a complete, though not that advanced, Totem Destroyer prototype.

Although you aren't able to determine if the idol touched the ground (failing the level), you can check for its angle to be between -30 and +30 to show that the idol did not fall down, and create your first Totem Destroyer game.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Box2D for Flash Games
Published in: Nov 2012 Publisher: Packt ISBN-13: 9781849519625
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}