gpt4 book ai didi

java - Box2d 中动态主体为空

转载 作者:行者123 更新时间:2023-12-01 04:20:16 25 4
gpt4 key购买 nike

我有一个动态 Sprite 和一些静态 Sprite 。我的动态 body :

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;

bodyDef.position.set(180, 20);
bodyMonkey = world.createBody(bodyDef);
PolygonShape abcBox = new PolygonShape();
bodyMonkey.setUserData(monkey1);
bodyMonkey.setFixedRotation(true);
abcBox.setAsBox(10.0f, 10.0f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = abcBox;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.9f;
Fixture fixture = bodyMonkey.createFixture(fixtureDef);
abcBox.dispose();
bodyMonkey.setLinearVelocity(new Vector2(1f, 0.5f));
bodyMonkey.setLinearDamping(1.0f);

我必须实现 ContactListener。我的类(class):

@Override
public void beginContact(Contact contact) {
Object a = contact.getFixtureA().getBody().getUserData();
Object b = contact.getFixtureB().getBody().getUserData();// null

Gdx.app.log("1", ""+a);
Gdx.app.log("2", ""+b);
if(a!=null&&b!=null) {
Gdx.app.log("ok", "");
screen2dBox.restartGame();
}
}

@Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub

}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub
}

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub
}

但是对象 b 始终为 null(a 不,b 是动态的,我在日志中看到它)。我尝试使用 try、catch、finally 来克服这个问题(在finally block 中 - screen2dBox.restartGame();),但我遇到了错误,联系人不能为空。不知道怎么解决?感谢您的帮助。

静态主体:

BodyDef bodyDefPlatform1 = new BodyDef();
bodyDefPlatform1.type = BodyType.StaticBody;
bodyDefPlatform1.position.set(50, 280);
bodyPlatform1 = world.createBody(bodyDefPlatform1 );
PolygonShape platformBox1 = new PolygonShape();
bodyPlatform1.setUserData(platform);
platformBox1.setAsBox(20.0f, 10.0f);
FixtureDef fixtureDefPlatform1 = new FixtureDef();
fixtureDefPlatform1.shape = platformBox1;
Fixture fixturePlatform = bodyPlatform1.createFixture( fixtureDefPlatform1);
platformBox1.dispose();

最佳答案

我检查了你的代码,没有发现任何错误。也许对象 Monkey1 为 null。

bodyMonkey.setUserData(monkey1); //check if monkey1 is null.

注意:我的声誉很低,我无法将此作为评论发布。

关于java - Box2d 中动态主体为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18987438/

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