简体   繁体   English

LIBGDX相机不动

[英]LIBGDX Camera doesn't move

My game sets this screen and I see the actor that I create. 我的游戏设置了此屏幕,并且看到了我创建的演员。

However can anyone tell me why the camera doesn't seem to be moving? 但是,谁能告诉我为什么相机似乎没有移动? I would expect the actor to move to the right as I pressed the Left key. 我希望演员在我按向左键时向右移动。

The Gdx.input.isKeyPressed(Input.Keys.LEFT) does resolve to true and the translate function does get called. Gdx.input.isKeyPressed(Input.Keys.LEFT)确实解析为true,并且平移函数也被调用。

public class MainScreen implements Screen {

    private Stage mainStage;
    private Camera orthCamera;

    @Override
    public void show() {


        orthCamera = new OrthographicCamera();

        mainStage = new Stage(new ScreenViewport());
        mainStage.getViewport().setCamera(orthCamera);

        MyGameActorObject s = new MyGameActorObject();
        s.setPosition(100, 100);

        mainStage.addActor(s);
    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(0,0,0,1); //sets clear
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        processKeyboardInput();

        mainStage.act(delta);
        mainStage.draw();
    }

    private void processKeyboardInput()
    {
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            orthCamera.translate(-30, 0, 0);
            orthCamera.update();
        }
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        mainStage.dispose();
    }
}

I am trying to acheieve being able to pan around the 2d game world using keyboard input. 我试图实现能够使用键盘输入在2d游戏世界中平移。

This is the contents of the custom Actor object: 这是自定义Actor对象的内容:

public class MyGameActorObject extends Actor
{
    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

        super.draw(batch, parentAlpha);

        ShapeRenderer r = new ShapeRenderer();

        r.begin(ShapeRenderer.ShapeType.Filled);
        r.setColor(Color.RED);
        r.circle(this.getX() - 50.0f, this.getY() - 50.0f, 100.0f);
        r.end();

    }
}

I believe I have to add something like 我相信我必须添加类似

r.setProjectionMatrix(batch.getProjectionMatrix());

in my custom actor object. 在我的自定义actor对象中。 So the draw function on my object would now be. 因此,现在我对象上的绘制函数将会是。

    @Override
    public void draw(Batch batch, float parentAlpha) {

        super.draw(batch, parentAlpha);

        ShapeRenderer r = new ShapeRenderer();

        r.setProjectionMatrix(batch.getProjectionMatrix());
        r.begin(ShapeRenderer.ShapeType.Filled);
        r.setColor(Color.RED);
        r.circle(this.getX() - 50.0f, this.getY() - 50.0f, 100.0f);
        r.end();

    }

I think your problem is that you haven't set the InputProcessor . 我认为您的问题是您尚未设置InputProcessor

Gdx.input.setInputProcessor(...) is the method you're looking for. Gdx.input.setInputProcessor(...)是您要寻找的方法。 Put it in your show() method and pass it an InputProcessor reference. 将其放在show()方法中,并传递一个InputProcessor引用。 InputProcessor is an interface that goes through all the different types of click-based input and allows you to determine what happens in them. InputProcessor是一个界面,它处理所有不同类型的基于点击的输入,并允许您确定其中发生的情况。 So for instance: 因此,例如:

@Override
public boolean keyDown(int keycode)
{
    switch(keycode)
    {
        case Keys.D:
            screen.universe.entity_handler.player.moving_right = true;
            break;
        case Keys.A:
            screen.universe.entity_handler.player.moving_left = true;
            break;
        case Keys.SPACE:
            screen.universe.entity_handler.player.jumping = true;
            break;
        ...
   }
   ...
}

The above code is part of an InputProcessor -implementing class for a game I'm making. 上面的代码是我正在制作的游戏的InputProcessor实现类的一部分。 When the input in-question is pressed ( keyDown in this case), the game looks at your input button (the keycode ) and sends it through the switch statement. 当按下输入问题时(在这种情况下为keyDown ),游戏将查看您的输入按钮( keycode ),并将其通过switch语句发送。

Keep in mind that you must return true for all the input events that you want to use. 请记住,对于所有要使用的输入事件,您必须返回true That's what the boolean return value for this method is. 这就是该方法的boolean返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM