如何在Box2D(cocos2d)中销毁b2Body?检查行驶距离后
我在 box2d/cocos2d-for-iphone 中有子弹。它们飞得很好……但我想在它们飞行一定距离后销毁这些子弹。例如,子弹“飞过”480px 后,应将其删除。
我怎样才能实现这个目标?
I have bullets in box2d/cocos2d-for-iphone. They are flying fine...but I want to destroy these bullets after they traveld a certain distance. for example after a bullet "flew" 480px it should be removed.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了计算距离,在创建子弹时将其位置存储在某处。然后每一步检查:
编辑:
如果你想计算路径长度,则将前一个位置和路径长度存储在某处,最初为0:
To count the distance, when creating a bullet store it's position somewhere. Then every step check:
EDIT:
if you want to calculate the path length then store somewhere the previous position and the path length, that is initially 0:
这非常简单:
world->DestroyBody(body)
。还有,小建议。为了获得良好的实践和性能,您不应该一遍又一遍地创建子弹。重复使用它!只需使它们不可见并将它们重新放置在源位置即可。
It's quite simple:
world->DestroyBody(body)
.And, small advice. For the good practice and performance you should not create bullets over and over again. Reuse it! Just make them invisible and reposition them at a position of a source.