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

Checking for collisions among bodies


To complete the prototype, we need to check whether the idol touches the ground. The simplest way, according to what you have learned about Box2D until now, is to continuously scan through idol collisions and check whether one of the bodies it collides with is the ground.

We need to add some lines to the update function:

update:function(dt){
  world.Step(dt,10,10);
  for (var b = world.GetBodyList(); b; b = b.GetNext()) {
    if (b.GetUserData() != null) {
      var mySprite = b.GetUserData().asset;
      mySprite.setPosition(b.GetPosition().x * worldScale, b.GetPosition().y * worldScale);
      mySprite.setRotation(-1 * cc.radiansToDegrees (b.GetAngle()));
      if(b.GetUserData().type=="totem"){
        for(var c = b.GetContactList(); c; c = c.m_next){
          if(c.other.GetUserData() && c.other.GetUserData().type=="ground"){
            console.log("Oh no!!!!");
          }
        }
      }
    }
  }
}

In the way that we looped through bodies...