Search code examples
javascriptchipmunkcocos2d-js

How to get member variables in collision detection functions?


I am working on a side scrolling game and using cocos2djs framework for that. Currently I am trying to build collision detection functions, but I hit a bump.

I want to update my HUD with some information regarding player's health, when a collision occurs, however, I am not able to access member variables from inside these functions for some reason.

This is my collision handler (I use chipmunk physics)

this.space.addCollisionHandler(pape,
                               obstacle,
                               this.collisionObstacleBegin,
                               null,
                               null,
                               null);

I handle the collision detection here addCollisionHandler:

addCollisionHandler: function() {
    cc.log(this.healthStatus);
}

But the console shows this.healthStatus as undefined, even though it is defined and has been used for setting up the HUD earlier in the init function. Can somebody help me out?


Solution

  • I found the mistake, I forgot to bind the collisionObstacleBegin...