简体   繁体   English

如何在AndEngine中删除碰撞的box2d体?

[英]How to remove colliding box2d bodies in AndEngine?

I'm using AndEngine to create a game with box2d extension. 我正在使用AndEngine创建一个带有box2d扩展名的游戏。 I want to implement a condition that when two bodies collide, they should be removed or respawned. 我想实现一个条件,当两个物体碰撞时,它们应该被移除或重生。 Initially I tried using 最初我尝试过使用

if(sprite1.collidesWith(sprite2))
// remove the sprites

but this doesnt seem to help because the sprites are attached to their respective bodies. 但这似乎没有帮助,因为精灵附着在各自的身体上。 I also tried using contactListener as follows: 我也尝试使用contactListener,如下所示:

bodyA=getFixtureA().getBody();
bodyB=getFixtureB().getBody();

and then later 然后是

if(bodyA!=null && bodyB!=null)
// remove the bodies

but that crashed the game. 但那场比赛很糟糕。 Does anybody know a better way of doing it? 有人知道更好的方法吗?

So there's some stuff out there about Collision but it took me quite awhile to figure out how to remove the objects based on collision. 所以有一些关于碰撞的东西,但我花了很长时间才弄清楚如何根据碰撞去除物体。 So decided to do a quick tutorial to try and help other people out. 所以决定做一个快速的教程来尝试帮助其他人。 The key is that in Box2D you cannot delete the body in the collision handler and need to do something to mark it for deletion to be handled on the next update. 关键是在Box2D中你不能删除碰撞处理程序中的正文,并且需要做一些事情来标记它以便在下次更新时处理删除。 (might be better ways but this works and gets the job done) (可能是更好的方法,但这有效并完成工作)

Here is link for a very nice tutorial. 这是一个非常好的教程的链接。

Box2D Collision and Removal Box2D碰撞和移除

This will surely help you to get your work done. 这肯定会帮助您完成工作。

Thanks :) 谢谢 :)

如果实际发生Sprite碰撞,您可以同时删除实体:

mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(pSprite);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM