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...