问题
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();
}
}
and here is main class just in case this is relevant:
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...
回答1:
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);
that line before the begin() line was all I needed to fix this.
来源:https://stackoverflow.com/questions/38935310/libgdx-camera-setting-position-having-no-effect