[英]how to get the sprite attached to a box2d body if i knew the body?
After collision detection I can get the bodies that collided, but how can I get the sprites attached to these bodies so I can remove them from the scene also? 进行碰撞检测后,我可以得到碰撞的物体,但是如何将精灵附着在这些物体上,这样我也可以将它们从场景中移除?
For example if I had a body (player) this body collided with another body (enemy), I can get which bodies collide from the contact listener like this: 例如,如果我有一个身体(播放器),这个身体与另一个身体(敌人)相撞,那么我可以从接触侦听器中获得哪些身体相撞,如下所示:
pContact.getFixtureA().getBody()
pContact.getFixtureB().getBody()
So if we assume that we have a lot of enemies how can I get the exact sprite attached to each one of the bodies so I can remove them? 因此,如果我们假设有很多敌人,如何才能将精确的精灵附着在每个尸体上,以便将它们移除?
disclaimer: My experience is with Cocos2d/Box2D in iOS, but should be similar in Android. 免责声明:我的经验是在iOS中使用Cocos2d / Box2D,但在Android中应该类似。
Typically, when using Box2D bodies and sprites you set the UserData property of the Body Definition with the sprite. 通常,在使用Box2D实体和精灵时,请使用该精灵设置主体定义的UserData属性。 Example:
例:
setting userData: 设置userData:
bodyDef.UserData = sprite;
retrieving sprite after contact: 联系后检索精灵:
enemySprite = pContact.getFixtureA().getBody().GetUserData()
You should have an Enemy
class, that has its Body
and Sprite
. 您应该有一个
Enemy
类,该类具有其Body
和Sprite
。 If you have a body that had a contact with an another body, you can iterate over the list of your Enemy
objects to find whose body was that by simply comparing them. 如果您有一个与另一个物体接触过的物体,则可以通过简单地比较它们来遍历您的
Enemy
对象列表,以找到其物体。 Then you can easily get this Enemy
object's sprite. 然后,您可以轻松获取此
Enemy
对象的精灵。 Hope I've explained my idea clear enough. 希望我已经足够清楚地解释了我的想法。
After read this thread on AndEngine forums, this is my solution: 在AndEngine论坛上阅读此线程后,这是我的解决方案:
Create a new "UserData" class: 创建一个新的“ UserData”类:
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
String mString;
Object mObject;
public UserData(){}
public UserData(String string, Object object) {
this.mString = string;
this.mObject = object;
}
public String getString() {
return mString;
}
public Object getObject() {
return mObject;
}
}
Set the UserData of all your body
s with the UserData class: 设置的所有的的UserData
body
s的使用用户数据类:
body.setUserData(new UserData("enemy", CustomSprite));
Then you can detect the collision and get your CustomSprite: 然后,您可以检测到碰撞并获取CustomSprite:
if (((UserData) x1.getBody().getUserData()).getString().equals("enemy") && ((UserData) x2.getBody().getUserData()).getString().equals("bullet")) {
CustomSprite myEnemySprite = (CustomSprite) ((UserData) x1.getBody().getUserData()).getObject();
myEnemySprite.die();
}
enemy.setUserData(new Object[]{"enemy",enemySprite/*,... And other objects as user data if you wish */})
And while checking: 并且在检查时:
final Object[] x1 = (Object[]) contact.getFixtureA().getBody().getUserData();
final Object[] x2 = (Object[]) contact.getFixtureB().getBody().getUserData();
Doing something: 做某事:
if(x1[0].equals("This is the tag for the enemy"))
x2[1].playerhascollidedwithyouandyouhavetodosomething();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.