我在将视口应用到项目时遇到了问题。下面的代码试图让控制面板移动球。但是,当我尝试使用视口时,我不知道该怎么做。所以我试图从头开始遵循游戏方法。我在构造函数中添加了以下代码。
camera = new OrthographicCamera();
viewport = new FitViewport(1280,720,camera);
viewport.apply();
camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
我一无所获。屏幕上没有任何内容。我尝试将相机位置设置到不同的位置,我什么也看不见。我的代码有什么问题?
public class GameScreen extends AbstractScreen {
private OrthographicCamera camera;
private Viewport viewport;
private MyInputHandler handler;
//Box2D
World world;
Box2DDebugRenderer b2dr;
Body body;
Body control;
Fixture fixture;
//Parameters for controls
private double angle;
private double ControlMagnitudeX;
private double ControlMagnitudeY;
private float fControlMagnitudeX;
private float fControlMagnitudeY;
public GameScreen(final HunterLife game) {
super(game);
this.camera = new OrthographicCamera();
camera.setToOrtho(false, HunterLife.V_WIDTH/PPM, HunterLife.V_HEIGHT/PPM);
this.world = new World(new Vector2(0f, 0f), true);
this.b2dr = new Box2DDebugRenderer();
handler = new MyInputHandler();
BodyDef controlDef = new BodyDef();
controlDef.type = BodyDef.BodyType.StaticBody;
controlDef.position.set(HunterLife.V_WIDTH/8/PPM, HunterLife.V_HEIGHT/5/PPM);
this.control = world.createBody(controlDef);
CircleShape controlCircle = new CircleShape();
controlCircle.setRadius (0.1f);
FixtureDef controlFixture = new FixtureDef();
controlFixture.shape = controlCircle;
controlFixture.isSensor = true;
control.createFixture(controlFixture);
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.DynamicBody;
bdef.position.set(HunterLife.V_WIDTH/2/PPM,HunterLife.V_HEIGHT/2/PPM);
FixtureDef fdef = new FixtureDef();
this.body = world.createBody(bdef);
CircleShape circle = new CircleShape();
circle.setRadius (0.05f);
fdef.shape = circle;
fdef.density = 1f;
fdef.friction = 0f;
fdef.restitution = 1f;
fixture = body.createFixture(fdef);
circle.dispose();
controlCircle.dispose();
}
@Override
public void show() {
}
@Override
public void update(float delta) {
world.step(1 / 60f, 6, 2);
if (Gdx.input.isTouched()) {
if (((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) - control.getPosition().y * PPM) * ((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) - control.getPosition().y * PPM) + ((Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) - control.getPosition().x * PPM) * ((Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) - control.getPosition().x * PPM) <= 0.25f * 0.25f * PPM * PPM) {
angle = Math.abs(Math.atan(((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) - control.getPosition().y * PPM) / ((Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) - control.getPosition().x * PPM)));
ControlMagnitudeX = 0.15f * Math.cos(angle);
ControlMagnitudeY = 0.15f * Math.sin(angle);
fControlMagnitudeX = MyInputHandler.getFloat(ControlMagnitudeX);
fControlMagnitudeY = MyInputHandler.getFloat(ControlMagnitudeY);
if ((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) >= control.getPosition().y * PPM && (Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) >= control.getPosition().x * PPM) {
body.setLinearVelocity(fControlMagnitudeX, fControlMagnitudeY);
}
if ((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) < control.getPosition().y * PPM && (Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) >= control.getPosition().x * PPM) {
body.setLinearVelocity(fControlMagnitudeX, -fControlMagnitudeY);
}
if ((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) >= control.getPosition().y * PPM && (Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) < control.getPosition().x * PPM) {
body.setLinearVelocity(-fControlMagnitudeX, fControlMagnitudeY);
}
if ((HunterLife.V_HEIGHT - Gdx.input.getY() * HunterLife.V_HEIGHT / Gdx.graphics.getHeight()) < control.getPosition().y * PPM && (Gdx.input.getX() * HunterLife.V_WIDTH / Gdx.graphics.getWidth()) < control.getPosition().x * PPM) {
body.setLinearVelocity(-fControlMagnitudeX, -fControlMagnitudeY);
}
}
else {
body.setLinearVelocity(0, 0);
}
}
else {
body.setLinearVelocity(0, 0);
}
stage.act();
}
@Override
public void render(float delta) {
super.render(delta);
game.sb.setProjectionMatrix(camera.combined);
game.shapeBatch.setProjectionMatrix(camera.combined);
camera.update();
b2dr.render(world, camera.combined);
stage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
super.dispose();
world.dispose();
}
}
请忽略舞台的事情。 上面的代码没有问题。添加视口内容时会出现问题。