Book Image

Learning Cocos2d-JS Game Development

By : Emanuele Feronato
Book Image

Learning Cocos2d-JS Game Development

By: Emanuele Feronato

Overview of this book

Table of Contents (18 chapters)
Learning Cocos2d-JS Game Development
Credits
Foreword
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Creating Your Own Blockbuster Game – A Complete Match 3 Game
Index

Adding bodies to the world


A physics object in the Box2D world is called a body. So, we will see how to add a body to the world. Moreover, since all bodies in our Totem Destroyer game are boxes, we will define a function to create a body and customize it to fit our needs.

We'll start from the end, calling a function we haven't written yet, just to have a look at all arguments that we need to create any kind of body used in Totem Destroyer.

So, the game's init function will be modified this way:

init:function () {
  this._super();
  var backgroundLayer = cc.LayerGradient.create(cc.color(0xdf,0x9f,0x83,255), cc.color(0xfa,0xf7,0x9f,255));
  this.addChild(backgroundLayer);
  var gravity = new Box2D.Common.Math.b2Vec2(0, -10)
  world = new Box2D.Dynamics.b2World(gravity, true);
  
  this.scheduleUpdate();
  this.addBody(240,10,480,20,false,"assets/ground.png","ground");
}

Our custom function is called addBody and according to the number of arguments will do a lot of things. Let's have a look at...