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

Selecting and destroying world bodies


As the name Totem Destroyer suggests, you should be able to destroy the totem. First, uncomment the previously commented line in order to give back the totem its left foot, and then we are ready to destroy bricks when the player touches/clicks them.

Everything starts with a touch, so we have to manage it by first adding the listener to the game's init function:

init:function () {
  // same as before
  cc.eventManager.addListener(touchListener, this);
}

Then create the listener variable itself:

var touchListener = cc.EventListener.create({
  event: cc.EventListener.TOUCH_ONE_BY_ONE,
  swallowTouches: true,
  onTouchBegan: function (touch, event) {
    var worldPoint = new Box2D.Common.Math.b2Vec2(touch.getLocation().x/worldScale,touch.getLocation().y/worldScale);
    for (var b = world.GetBodyList(); b; b = b.GetNext()) {
      if (b.GetUserData() != null && b.GetUserData().type=="destroyable") {
        for(var f = b.GetFixtureList();f; f=f.GetNext...