I am working on a project with friends and I made my part which was to do with viewports and cameras. When I tested it the camera was fine and moved fine, but now we put our parts together and I had to move some of my code to a PlayState class. Now the sprites are moving, but the camera isn't even set right from the start and it looks like it can't move. What is the problem here? Here is some code:
Game class:
package com.platformer.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.platformer.managers.GameStateManager;
public class Game implements ApplicationListener{
private GameStateManager gsm;
SpriteBatch batch;
final float WIDTH=480;
final float HEIGHT=320;
float dx,dy;
public Viewport viewport;
public OrthographicCamera camera;
public void create () {
gsm = new GameStateManager();
batch = new SpriteBatch();
float aspectRatio=(float)Gdx.graphics.getWidth()/(float)Gdx.graphics.getHeight();
camera=new OrthographicCamera();
viewport=new FitViewport(HEIGHT*aspectRatio,HEIGHT,camera);
viewport.apply();
}
public void render () {
//clear frame
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.translate(dx,dy);
//update and draw gamestate
gsm.update(Gdx.graphics.getDeltaTime());
gsm.draw();
camera.update();
batch.begin();
batch.setProjectionMatrix(camera.combined);
batch.end();
}
public void resize(int width, int height) {
viewport.update(width,height);
camera.position.set(WIDTH,HEIGHT/2,0);
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
public void setCamera(float dx,float dy){
this.dx=dx;
this.dy=dy;
}
public float getWIDTH(){return WIDTH;}
public float getHEIGHT(){return HEIGHT;}
}
PlayState class:
package com.platformer.gamestates;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.platformer.game.Game;
import com.platformer.managers.GameStateManager;
public class PlayState extends GameState{
SpriteBatch batch;
Sprite right,left,background,character;
float dx,dy;
Game game=new Game();
float lx=game.getWIDTH()/2+5,ly=5,cx=game.getWIDTH()/2+165,cy=45;
public PlayState(GameStateManager gsm){
super(gsm);
}
public void init(){
dx=0;
dy=0;
batch = new SpriteBatch();
background=new Sprite(new Texture(Gdx.files.internal("happyplace.png")));
right=new Sprite(new Texture(Gdx.files.internal("go_right.png")));
left=new Sprite(new Texture(Gdx.files.internal("go_left.png")));
character=new Sprite(new Texture(Gdx.files.internal("char.png")));
}
public void update(float dt){
handleInput();
left.setPosition(lx,ly);
right.setPosition(lx+80,ly);
character.setPosition(cx,cy);
game.setCamera(dx,dy);
dx=0;
dy=0;
}
public void draw(){
batch.begin();
background.draw(batch);
right.draw(batch);
left.draw(batch);
character.draw(batch);
batch.end();
}
public void handleInput(){
//some movement bits
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){dx+=5f;lx+=5f;}
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){dx-=5f;lx-=5f;}
if(Gdx.input.isKeyPressed(Input.Keys.UP)){dy+=5f;ly+=5f;}
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){dy-=5f;ly-=5f;}
if(Gdx.input.isTouched()){
if(Gdx.input.getX()<40
&&Gdx.input.getY()>game.getHEIGHT()-40){dx-=5f;lx-=5f;cx-=5f;}
if(Gdx.input.getX()<120
&&Gdx.input.getX()>80
&&Gdx.input.getY()>game.getHEIGHT()-40){dx+=5f;lx+=5f;cx+=5f;}
}
}
public void dispose(){
background.getTexture().dispose();
left.getTexture().dispose();
right.getTexture().dispose();
}
}