Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to send prototype method as a callback in javascript?
  2. Box2d.prototype.getBodyAtMouse = function() {
  3.    this.mousePVec = new this.b2Vec2(this.mouseX/this.SCALE, this.mouseY/this.SCALE);
  4.    var aabb = new this.b2AABB();
  5.    aabb.lowerBound.Set(this.mouseX/this.SCALE - 0.001, this.mouseY/this.SCALE - 0.001);
  6.    aabb.upperBound.Set(this.mouseX/this.SCALE + 0.001, this.mouseY/this.SCALE + 0.001);
  7.    this.selectedBody = null;
  8.    var _this = this;
  9.    this.world.QueryAABB(_this.getBodyCB, aabb);
  10.    return this.selectedBody;
  11.        
  12. b2World.prototype.QueryAABB = function (callback, aabb) {
  13.   var __this = this;
  14.   var broadPhase = __this.m_contactManager.m_broadPhase;
  15.  
  16.   function WorldQueryWrapper(proxy) {
  17.      return callback(broadPhase.GetUserData(proxy));
  18.   };
  19.   broadPhase.Query(WorldQueryWrapper, aabb);
  20.        
  21. Box2d.prototype.getBodyCB = function(fixture)
  22. {
  23.   if(fixture.GetBody().GetType() != this.b2Body.b2_staticBody) {
  24.    if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), this.mousePVec)) {
  25.       selectedBody = fixture.GetBody();
  26.       return false;
  27.    }
  28. }
  29. return true;
  30.        
  31. var _this = this;
  32. this.world.QueryAABB(function(fix){
  33.    _this.getBodyCB(fix);
  34. }, aabb);
  35.        
  36. this.world.QueryAABB(this.getBodyCB.bind(this), aabb);