Libgdx相机-设置位置无效 [英] Libgdx Camera - Setting position having no effect
问题描述
我一生都无法弄清楚为什么设置相机位置(在create方法和update方法中)都无效.
I cannot for the life of me figure out why my setting the camera position (both in the create method and the update method) have no effect.
我已经检查过的主类正在调用此更新.这是代码:
The update is being called by my main class I already checked. Here is code:
package com.moneylife.zombietown;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameManager {
SpriteBatch spriteBatch;
float deltaTime;
Texture mapTexture;
OrthographicCamera camera;
int WORLDWIDTH = 2880, WORLDHEIGHT = 1920;
public GameManager(){
spriteBatch = new SpriteBatch();
mapTexture = new Texture("map2880x1920.jpg");
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(940, 940 * (h / w)); /// POSSIBLY NEED TO CHECK THIS *******
camera.position.set(camera.viewportWidth/2 + 500, camera.viewportHeight/2, 0);
camera.update();
}
public void update(){
deltaTime = Gdx.graphics.getDeltaTime();
camera.position.x += 10;
camera.position.y += 10;
camera.update();
}
public void draw(){
spriteBatch.begin();
spriteBatch.draw(mapTexture,0,0);
spriteBatch.end();
}
}
这是主类,以防相关:
package com.moneylife.zombietown;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ZombieTown extends ApplicationAdapter {
GameManager gameManager;
@Override
public void create () {
gameManager = new GameManager();
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameManager.update();
gameManager.draw();
}
@Override
public void dispose () {
gameManager.spriteBatch.dispose();
}
}
我现在只是想让地图在启动时自动对角滚动...
I was just trying to make the map automatically scroll diagonally upon startup for now...
推荐答案
Menno Gouw回答了这个问题.他在上面的评论中指出,我没有将相机的投影矩阵添加到spritebatch中.
This question was answered by Menno Gouw. His comment above states that I didn't add the projection matrix of the camera to the spritebatch.
spriteBatch.setProjectionMatrix(camera.combined);
begin()行之前的那一行是我解决此问题所需的全部内容.
that line before the begin() line was all I needed to fix this.
这篇关于Libgdx相机-设置位置无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!