【发布时间】:2015-10-28 11:15:37
【问题描述】:
我目前正在创建一个 2d 无限奔跑游戏。在我的游戏中,我有两个演员,英雄和硬币,他们的身体都设置为动态,但每次英雄击中/碰撞硬币时,英雄都会向左移动(英雄会受到硬币的影响)。
这是我的代码,我不知道我的设置是否有问题,任何帮助,将不胜感激。
public static Body createRunner(World world) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
PolygonShape shape = new PolygonShape();
shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
Body body = world.createBody(bodyDef);
body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);
body.setUserData(new RunnerUserData());
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 0.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
body.createFixture(fixtureDef);
body.resetMassData();
body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));
shape.dispose();
return body;
}
public static Body createCoins(World world) {
EnemyType enemyType = RandomUtils.getRandomEnemyType();
BodyDef bodyDef = new BodyDef();
//bodyDef used to set the enemy to make it not be affected when the runner hit
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
PolygonShape shape = new PolygonShape();
shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
Body body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = -100.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = -50.0f;
body.createFixture(fixtureDef);
body.resetMassData();
EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getRegions());
body.setUserData(userData);
shape.dispose();
return body;
}
谢谢
【问题讨论】:
标签: java android libgdx game-physics