java - Actor 远离摄像机而不是在 LibGdx 中沿 X 轴移动

标签 java camera libgdx

我想要一个摄像机跟随 Actor 。我看过很多关于如何执行此操作的教程,但我遇到了一个奇怪的错误。 我创建的名为“骨架”的 Actor 似乎远离相机,而不是在 X 轴上。 相机在静态 Sprite 上移动得很好。

我使用了 3 种不同类型的移动 Actor 。它们似乎都不起作用。 也为糟糕的代码感到抱歉。

播放画面:

public class PlayScreen implements Screen {
    OrthographicCamera camera;
    Table table;
    Stage stage;
    Viewport viewport;
    private MyGdxGame game;
    skeleton skeleton;

    public PlayScreen(MyGdxGame game){
        this.game = game;
        camera = new OrthographicCamera(1f, (Gdx.graphics.getHeight()/Gdx.graphics.getWidth()));
        viewport =new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),camera);
        skeleton = new skeleton();
        stage = new Stage(new ScreenViewport());
        skeleton.setPosition((Gdx.graphics.getWidth()/2)-(skeleton.getWidth()/2),((Gdx.graphics.getHeight()/1.75f)-(skeleton.getHeight()/1.4f))); 
        stage.addActor(skeleton);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        skeleton.moveRight();
        camera.position.x = skeleton.getX() + skeleton.getOriginX();
        camera.position.y = skeleton.getY() + skeleton.getOriginY();

        camera.update();
        game.batch.setProjectionMatrix(camera.combined);
        Gdx.input.setInputProcessor(stage);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width,height);
    }

骷髅 Actor :

public class skeleton extends Actor {
    SpriteBatch batch;
    Texture img;
    int frame = 0;
    int zeile = 0;
    TextureRegion[][] regions;
    public Sprite sprite;

    public skeleton(){
        batch = new SpriteBatch();
        img = new Texture(Gdx.files.internal("Warrior Skeleton Animations/Walking/walk.png"));

        //ANIMATION
        regions = TextureRegion.split(img,572,953);
        sprite = new Sprite(regions[0][0]);

        code to make it an animation...
        (works fine don't wont to enlarge the code here any further)

        setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());

    }

    public void moveRight(){
        //First way of moving
        MoveByAction mba = new MoveByAction();
        mba.setAmount(10f,0f);
        mba.setDuration(1/10f);
        skeleton.this.addAction(mba);

        //Second way of moving
        //this.setPosition(this.getX()+10,this.getY());
        //setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());

        //Third way of moving
        //sprite.translateX(1);
        //setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
    }

    @Override
    protected void positionChanged() {
        sprite.setPosition(getX(),getY());
        super.positionChanged();
    }

    @Override
    public void draw(Batch batch, float parentAlpha){
        sprite.draw(batch);
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }
}

我尝试过独立于骨架 Actor 移动相机,如下所示:camera.position.x =camera.position.x + 100;即使相机移动速度比 Actor 快, Actor 仍然会远离相机。

我还尝试使用骨架 Actor 的 Sprite 本身的坐标来移动相机。但同样的错误。

提前致谢。

最佳答案

我找到了解决这个问题的方法。

如果我移动相机本身而不是 Actor ,我会得到 Actor 被移动的效果。这证明我的 Actor 已绑定(bind)到我的相机。

我仍然很想知道这是为什么以及如何解决它。

关于java - Actor 远离摄像机而不是在 LibGdx 中沿 X 轴移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678385/

相关文章:

使用 EJB 计时器的 Java EE 调度程序任务

ios - SWIFT 3 : Capture photo with AVCapturePhotoOutput (Need another set of eyes to look over code, 为什么这不起作用?)

java - Android相机 Intent 保存文件两次/如何避免

Cordova:调整相机插件会使应用程序和相机崩溃

libgdx - LibGDX如何判断当前操作系统运行的是哪种语言?

java - 基于 JVM 的语言/运行时如何生成 Java 字节码?

java - 映射的子错误:AWS EMR实例终止

java - 正则表达式:包含一个词,不包含另一个词

java - 如何使矩形改变其位置或移动?

android - 带有正交相机的 libgdx 视口(viewport)段