I've been following the "How to make a 2D game" here, but it wasn't shown how make a camera follow a specific sprite.
My rendering code is as follows
public class [ClassName] {
polkymain game;
OrthographicCamera camera;
public static int PolkyX;
public static int PolkyY;
SpriteBatch batch;
public GameScreen(polkymain game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(true, 1280, 1240);
batch = new SpriteBatch();
PolkyX = 0;
PolkyY = 0;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.95F, 0.95F, 0.95F, 0.95F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
generalUpdate();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(Assets.Sprite_Mario_main, PolkyX, PolkyY);
batch.end();
}
public void generalUpdate(){
if(Gdx.input.isKeyPressed(Keys.D) || (Gdx.input.isKeyPressed(Keys.LEFT))
{
PolkyX += 5;
}
if(Gdx.input.isKeyPressed(keys.A) || (Gdx.input.isKeyPressd(Keys.RIGHT))
{
PolkyX -= 5;
}
if(Gdx.input.isKeyPressed(keys.S) || (Gdx.input.isKeyPressd(Keys.DOWN))
{
PolkyY -= 5;
}
if(Gdx.input.isKeyPressed(keys.W) || (Gdx.input.isKeyPressd(Keys.UP))
{
PolkyY += 5;
}
}
@Override
public void resize(int width, int height) { /* TODO */ }
@Override
public void show() { /* TODO */ }
@Override
public void hide() { /* TODO */ }
@Override
public void pause() { /* TODO */ }
@Override
public void resume() { /* TODO */ }
@Override
public void dispose() { /* TODO */ }
}
Also, I have an "Assets" class with all my textures and sprites coding, don't know if this is relevant. I don't think so now that I think about it.
[ClassName]
at the top and any other missing information you might have missed. – krillgar