2012-07-26 88 views
0

我有一个由我使用的Box2D API的回调激活的原型方法。回调可以到达函数,但它不是函数的正确实例。什么是正确的语法来获得回调调用。原型类。如何发送原型方法作为JavaScript中的回调?

的Box2D的API调用:

Box2d.prototype.getBodyAtMouse = function() { 
    this.mousePVec = new this.b2Vec2(this.mouseX/this.SCALE, this.mouseY/this.SCALE); 
    var aabb = new this.b2AABB(); 
    aabb.lowerBound.Set(this.mouseX/this.SCALE - 0.001, this.mouseY/this.SCALE - 0.001); 
    aabb.upperBound.Set(this.mouseX/this.SCALE + 0.001, this.mouseY/this.SCALE + 0.001); 
    this.selectedBody = null; 
    var _this = this; 
    this.world.QueryAABB(_this.getBodyCB, aabb); 
    return this.selectedBody; 

}

这是被称为Box2D的API方法:

b2World.prototype.QueryAABB = function (callback, aabb) { 
    var __this = this; 
    var broadPhase = __this.m_contactManager.m_broadPhase; 

    function WorldQueryWrapper(proxy) { 
    return callback(broadPhase.GetUserData(proxy)); 
    }; 
    broadPhase.Query(WorldQueryWrapper, aabb); 

}

这是回调我希望API能够正确引用:

Box2d.prototype.getBodyCB = function(fixture) 
{ 
    if(fixture.GetBody().GetType() != this.b2Body.b2_staticBody) { 
    if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), this.mousePVec)) { 
     selectedBody = fixture.GetBody(); 
     return false; 
    } 
} 
return true; 

}

回答

0

要么使用

var _this = this; 
this.world.QueryAABB(function(fix){ 
    _this.getBodyCB(fix); 
}, aabb); 

this.world.QueryAABB(this.getBodyCB.bind(this), aabb); 

which不被旧版本浏览器的支持,但很容易被垫高)

相关问题