Reputation: 808
I want to adjust the texture in my game to diffrent screens sizes, so I try to add ExtendViewport
into my viewport
, but Whenever I try change the states the background is not loaded. For the first time everything works fine, but whenever I try to set another state Ive got blank screen (I try with the same State instance to exclude the code errors)
STATE
public abstract class State {
protected OrthographicCamera camera;
protected GameStateManager gsm;
protected Viewport viewport;
public State(GameStateManager gsm) {
this.gsm = gsm;
camera = new OrthographicCamera();
viewport = new ExtendViewport(GAME_SIZE_WIDTH * aspectRatio, GAME_SIZE_HEIGHT, camera);
viewport.apply();
camera.position.set(GAME_SIZE_WIDTH / 2, GAME_SIZE_HEIGHT / 2, 0);
}
public abstract void handleInput();
public abstract void update(float dt);
public abstract void render (SpriteBatch sb);
public abstract void dispose();
public abstract void resize(int width, int height) ;
}
Menu State
public class MenuState extends State{
private Sprite background;
private Sprite playButton;
public MenuState(GameStateManager gsm) {
super(gsm);
background =new Sprite(new Texture(Gdx.files.internal("back.jpg")));
background.setSize(GAME_SIZE_WIDTH,GAME_SIZE_HEIGHT);
background.setPosition(0,0);
playButton=new Sprite(new Texture("play.png"));
playButton.setSize(40,30);
playButton.setPosition(GAME_SIZE_WIDTH/2-playButton.getWidth()/2,GAME_SIZE_HEIGHT/2-playButton.getHeight()/2);
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(camera.combined);
sb.begin();
background.draw(sb);
playButton.draw(sb);
sb.end();
}
@Override
public void dispose() {
playButton.getTexture().dispose();
background.getTexture().dispose();
}
@Override
public void resize(int width, int height) {
viewport.update(width,height);
camera.position.set(GAME_SIZE_WIDTH / 2, GAME_SIZE_HEIGHT / 2, 0);
}
@Override
public void handleInput() {
if(Gdx.input.justTouched()) {
gsm.set(new MenuState(gsm));}
}
@Override
public void update(float dt) {
handleInput();
}
}
GameStateManager
public class GameStateManager {
private Stack<State> states;
public GameStateManager() {
states=new Stack<State>();
}
public void push(State state){
states.push(state);
}
public void pop(){
states.pop().dispose();
}
public void set(State state){
states.pop().dispose();
states.push(state);
}
public void update(float dt){
states.peek().update(dt);
}
public void render(SpriteBatch sb){
states.peek().render(sb);
}
public void resize(int width, int height){
states.peek().resize(width,height);
}}
Upvotes: 0
Views: 74
Reputation: 20140
Problem is simple, you're not updating viewport of new State so update that with your screen width and height.
First time when Game Start resize method of State
called by ApplicationListener
class in this way.
ApplicationListener
's resize(,)
-> GameStateManager
's resize(,)
-> State
's resize(,)
After that resize( , )
of State or GameStateManager never called.
so simply change set()
method of your GameStateManager
.
public void set(State state){
state.resize((int)states.peek().viewport.getScreenWidth(),(int)states.peek().viewport.getScreenHeight());
states.pop().dispose();
states.push(state);
}
Upvotes: 1