gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:39 25 4
gpt4 key购买 nike

我想要一个摄像机跟随 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com