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 space bodies


The player must be able to destroy certain bodies: the ones called with the destroyable name by clicking or tapping over them. Thus, this is the complete touchListener declaration:

var touchListener = cc.EventListener.create({
  event: cc.EventListener.TOUCH_ONE_BY_ONE,
  onTouchBegan: function (touch, event) {
    for(var i=shapeArray.length-1;i>=0;i--){
      if(shapeArray[i].pointQuery(cp.v(touch.getLocation().x,touch.getLocation().y))!=undefined){
        if(shapeArray[i].name=="destroyable"){
          world.removeBody(shapeArray[i].getBody())
          world.removeShape(shapeArray[i])
          shapeArray.splice(i,1);
        }
      }
    }
  }
})

Before commenting on it, I will explain to you another way to iterate through all these bodies or shapes in the space.

Do you remember body selection in Box2D? We looped through all world bodies using the GetBodyList() function. That's one way to do it. However, there are other ways; since I want to...