gpt4 book ai didi

android - 如何检测是否在 jBox2D 中触摸了特定的物体

转载 作者:行者123 更新时间:2023-11-29 02:00:22 25 4
gpt4 key购买 nike

我正在开发一款使用 jbox2d 和 jBox2d for android 的游戏。我想检测用户是否触摸了我世界中各种物体中的特定动态物体。我试过遍历所有 body 并找到我感兴趣的一个,但它对我不起作用。请帮忙这是我所做的:

@Override
public boolean ccTouchesEnded(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(),
event.getY()));

for(Body b = _world.getBodyList();b.getType()==BodyType.DYNAMIC; b.getNext())
{
CCSprite sprite = (CCSprite)b.getUserData();
if(sprite!=null && sprite instanceof CCSprite)
{
CGRect body_rect = sprite.getBoundingBox();
if(body_rect.contains(location.x, location.y))
{
Log.i("body touched","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
expandAndStopBody(b);
break;
}

}
}
return true;
}

触摸后,系统继续打印 GC_CONCURRENT freed 1649K, 14% free 11130K/12935K, paused 1ms+2ms and everything goes to hang like state.

最佳答案

要检查 body 是否被触摸,您可以使用世界对象的方法queryAABB。我尝试重新安排您的代码以使用该方法:

// to avoid creation every time you touch the screen
private QueryCallback qc=new QueryCallback() {

@Override
public boolean reportFixture(Fixture fixture) {
if (fixture.getBody()!=null)
{
Body b=fixture.getBody();
CCSprite sprite = (CCSprite)b.getUserData();
Log.i("body touched","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
expandAndStopBody(b);
}
return false;
}
};

private AABB aabb;

@Override
public boolean ccTouchesEnded(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));

// define a bounding box with width and height of 0.4f
aabb=new AABB(new Vec2(location.x-0.2f, location.y-0.2f),new Vec2(location.x+0.2f, location.y+0.2f));
_world.queryAABB(qc, aabb);

return true;
}

我尝试减少垃圾收集器,但必须实例化一些东西。有关 http://www.iforce2d.net/b2dtut/world-querying 的更多信息

关于android - 如何检测是否在 jBox2D 中触摸了特定的物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12986388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com