[英]Camera not following my player
我已經看到許多與這些完全相同的問題,但答案相同,但對我來說從來沒有用。 我有一個球員,他的位置向量不斷更新。 我需要我的相機不斷跟隨我的球員位置。 這是播放器類
public void update(){
//velocity.y += MainScreen.GRAVITY.y;
velocity.x += MainScreen.GRAVITY.x;
oldPosition = getGridPosition();
position.x += velocity.x;
position.y += velocity.y;
if(oldPosition.x == getGridPosition().x){
hasMoved = false;
System.out.println("Hasn't moved");
System.out.println("normX- " + position.x);
System.out.println("oldX- " + oldPosition.x);
}
else{
hasMoved = true;
System.out.println("Has moved");
}
if(Gdx.input.isKeyPressed(Keys.A)){
velocity.x -= 10 * Gdx.graphics.getDeltaTime();
}
else if(Gdx.input.isKeyPressed(Keys.D)){
velocity.x += 10 * Gdx.graphics.getDeltaTime();
}else
velocity.x = 0;
}
我試圖將相機位置設置為我的球員位置,但它不適合我。 這是我的主要渲染循環。
public void render () {
player.update();
camera.position.set(player.getPosition(), 0);
camera.update();
mapGrid.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mapGrid.render(tmpRend);
player.render(tmpRend);
}
我不知所措我不知道我做錯了什么,這是我在創建方法中初始化相機的方法
player = new Player();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false);
//camera.position.set(MapGrid.MAP_WIDTH / 2, MapGrid.MAP_HEIGHT, 0);
camera.position.set(player.getPosition(), 0);
camera.zoom = 2f;
camera.update();
tmpRend = new ShapeRenderer();
tmpRend.setAutoShapeType(true);
tmpRend.setProjectionMatrix(camera.combined);
input = new InputListener(camera);
Gdx.input.setInputProcessor(input);
拜托,我已經看到了許多其他類似的問題,我正在按照他們說的做,但我的就是行不通。 這是一張圖片來顯示會發生什么。
我只是不知道為什么當我遵循了我被告知的一切時它不起作用。 您能給我的任何幫助將不勝感激!
我認為您已經更新了錯誤的相機 - 您已經創建了它的一個實例,但是它與程序的其他部分連接在哪里?
使用Scene2d來管理您的演員、視口和攝像機。 你可以在這里讀更多關於它的內容:
https://github.com/libgdx/libgdx/wiki/Scene2d
您正在創建新舞台,將玩家(應該從Actor類繼承)添加到它,然后只是創建新的相機而不是創建您當前的舞台相機,就像
stage.getCamera()
你應該在渲染中做的是
...
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.viewport.update(this.screenWidth, this.screenHeight);
this.stage.act();
this.stage.draw();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.