最近这几天在弄box2d 获取指定坐标点下的刚体,是用回调方法来实现的,为什么死活没有回调?
代码如下getBodyCB死后没有执行。
getBodyAt:function(px,py){
cc.log(“getBodyAt”);
//转换鼠标坐标单位,除以30从m该为px
this. mousePVec = new Box2d.b2Vec2(px/30,py/30);
var aabb = new Box2d.b2AABB();
aabb.lowerBound.Set(px - 0.001, py - 0.001);
aabb.upperBound.Set(px + 0.001, py + 0.001);
this.selectedBody = null;
this.world.QueryAABB(this.getBodyCB, aabb);
return this.selectedBody;
},
getBodyCB:function(fixture) {
cc.log(“22222”);
if(fixture.GetBody().GetType() != Box2d.b2Body.b2_staticBody) {
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), this.mousePVec)) {
this.selectedBody = fixture.GetBody();
return false;
}
}
return true;
}
QueryAABB()method
public function QueryAABB(callback:Function, aabb:b2AABB):void
Query the world for all fixtures that potentially overlap the
provided AABB.
Parameters
callback:Function — a user implemented callback class. It should match signature
function Callback(fixture:b2Fixture):Boolean
Return true to continue to the next fixture.
aabb:b2AABB — the query box.
使用QueryPoint 回调方法也不执行
getBodyAt:function(px,py){
//转换鼠标坐标单位,除以30从m该为px
var mouseVector = new Box2d.b2Vec2(px/30,py/30);
//鼠标下的刚体
var bodyAtMouse = null;
//queryPoint函数中要用到的回调函数,注意,它必须有一个b2Fixture参数
function callBack(fixture) {
if ( fixture == null) return;
//如果fixture不为null,设置为鼠标下的刚体
bodyAtMouse = fixture.GetBody();
}
//利用QueryPoint方法查找鼠标滑过的刚体
this.world.QueryPoint(callBack, mouseVector);
//返回找到的刚体
return bodyAtMouse;
}
QueryPoint()method
public function QueryPoint(callback:Function, p:b2Vec2):void
Query the world for all fixtures that contain a point.
Note: This provides a feature specific to this port.
Parameters
callback:Function — a user implemented callback class. It should match signature
function Callback(fixture:b2Fixture):Boolean
Return true to continue to the next fixture.
p:b2Vec2