在我的游戏中,我将对象表示为演员,因此所有游戏对象都将在舞台上。出于某种原因,当我试图移动舞台的相机时,它将无法工作,或实际上它似乎不起作用。我已将游戏Actor添加到0,0的位置。当我翻译相机的位置时,演员仍然停留在左下角,尽管我记录了相机的位置,但它显示相机已移动。
public class Striker extends Actor {
private Sprite img;
private World worldRef;
private Body body;
//constructor
public Striker(float size, float x, float y, World world) {
img = new Sprite(new Texture(Gdx.files.internal("Striker.png")));
//mains the aspect size ratio
img.setSize((275f / 300f) * size, size);
img.setPosition(x, y);
worldRef = world;
//set up the physics
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(x,y);
body = world.createBody(bodyDef);
}
@Override
public void draw(Batch batch, float parentAlpha) {
img.draw(batch);
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public float getX() {
return body.getPosition().x;
}
@Override
public float getY() {
return body.getPosition().y;
}
@Override
public float getWidth() {
return img.getWidth();
}
@Override
public float getHeight() {
return img.getHeight();
}
}
2个日志的结果显示相机的位置已移动,但看起来不像。
public class StrikerScreen implements Screen {
public static float WIDTH = 1920;
public static float HEIGHT = 1080;
public static float PPM = 200;
private Launcher launcherRef;
private OrthographicCamera camera;
private FitViewport viewport;
private World world;
private Box2DDebugRenderer debugRenderer;
private Striker striker;
private Stage gameStage;
//constructor
public StrikerScreen(Launcher launcher) {
launcherRef = launcher;
world = new World(new Vector2(0, -9.8f), true);
debugRenderer = new Box2DDebugRenderer();
gameStage = new Stage();
camera = (OrthographicCamera) gameStage.getCamera();
viewport = new FitViewport(WIDTH / PPM, HEIGHT / PPM, gameStage.getCamera());
viewport.apply();
gameStage.setViewport(viewport);
striker = new Striker(160f / PPM, 0, 0, world);
gameStage.addActor(striker);
gameStage.getCamera().translate(viewport.getWorldWidth() / 2f, 500f, 0);
viewport.apply();
camera.update();
Gdx.app.log("StrikerScreen.java", "Camera position: " + gameStage.getCamera().position.toString());
Gdx.app.log("StrikerScreen.java", "Camera size: " + gameStage.getCamera().viewportWidth + ", " + gameStage.getCamera().viewportHeight);
}
@Override
public void show() {
}
public void update(float delta) {
world.step(1 / 30f, 6, 2);
gameStage.act(delta);
}
@Override
public void render(float delta) {
update(delta);
debugRenderer.render(world, camera.combined);
gameStage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height, true);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
答案 0 :(得分:0)
在您发布的代码中,您移动摄像机的唯一时间是在StrikerScreen构造函数中,您可以在其中明确地翻译它,并在resize
方法中,您已调用viewport.update(width, height, true);
传递{{1 to true
告诉它将摄像机移动到视口角落左下角(0,0)的位置。当您将屏幕设置为此屏幕时,会自动调用viewport.update
,这是您将相机设置为最近的位置。