How do I detect an object collided another ?


#1

Since cocos2d html5 is pretty much event based how do I detect such collision ?

I use moveto on a sprite and want to know when it collided with another… The tutorial shows the standard check on every frame using the update method but that is not valid for the html5 version, unless there’s a way to do it ?

edit: What I did was use the scheduler on the director to check on everyframe for collision, still wonder if that’s the correct way though ?


#2

New question :

I’m making a callback to tell the target it was hit, he has a method getHit that takes a parameter, and I call him like this

actions[ 1 ] = cc.CallFunc.create(this.target.getHit, this.target, 2);

I then use shot.runAction(cc.Sequence.create(actions)); to make the shot move, hit the target then destroy itself.

Problem is the 2 number, when getHit is called the parameter is not the 2 I passed, it’s some weird class that came from nowhere, any tips ?

edit:Nevermind, turns out that cocos for some reason is making a ghost parameter as the first parameter in the callback, so I had to change my method to work with it :
getHit: function (sender, damage) {

Now the damage is correct.


#3

use cc.rect to create rectangle objects . you can rectangle collision using cc.rectIntersectsRect(rect1,rect2){}

It returns true or false.

Good luck.