Deleting Buffers LibGdx - java

I have created multiple Screens for my Libgdx game.
Whenever I exit the game (Desktop Version), I am getting a red sentence in eclipse console, showing Deleting Buffers(1).
The number is changing according to the Screen, which is active when I quit the game.
What is this "Deleting Buffers"?
Do I have to worry about that?
Also the android version of my game crashes at the second Screen, while the desktop version is running perfectly.
Thanks in advance.
Screen1
public class Splash implements Screen {
private SpriteBatch batch;
private Sprite titleSprite;
private Input input;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
titleSprite.draw(batch);
batch.end();
Gdx.input.setInputProcessor(input);
}
#Override
public void show() {
batch = new SpriteBatch();
titleSprite = new Sprite(new Texture("img/Title.jpg"));
titleSprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
titleSprite.setOrigin(titleSprite.getWidth() / 2f,
titleSprite.getHeight() / 2f);
titleSprite.setPosition(
Gdx.graphics.getWidth() / 2f - titleSprite.getWidth() / 2f,
Gdx.graphics.getHeight() / 2f - titleSprite.getHeight() / 2f);
input = new Input() {
public boolean keyDown(int keycode) {
if (keycode == Keys.ENTER) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new MainMenu());
Gdx.audio.newMusic(Gdx.files.internal("sounds/button.mp3"))
.play();
dispose();
}
if (keycode == Keys.ESCAPE) {
Gdx.app.exit();
}
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer,
int button) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new MainMenu());
dispose();
return super.touchDown(screenX, screenY, pointer, button);
}
};
}
#Override
public void dispose() {
batch.dispose();
titleSprite.getTexture().dispose();
}
}
Screen2
public class MainMenu implements Screen {
private Stage stage;
private Table table;
private TextButton playButton, exitButton;
private Skin skin;
private BitmapFont white, black;
private TextureAtlas atlas;
private TextButtonStyle textButtonStyle;
private SpriteBatch batch;
private Sprite sprite;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
stage.act(delta);
stage.draw();
Gdx.input.setInputProcessor(stage);
if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
Gdx.app.exit();
}
}
#Override
public void show() {
batch = new SpriteBatch();
sprite = new Sprite(new Texture("img/Title.jpg"));
sprite.setOrigin(sprite.getWidth() / 2f, sprite.getHeight() / 2f);
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
sprite.setPosition(Gdx.graphics.getWidth() / 2f - sprite.getWidth()
/ 2f, Gdx.graphics.getHeight() / 2f - sprite.getHeight() / 2f);
sprite.setRotation(0);
white = new BitmapFont(Gdx.files.internal("fonts/White.fnt"), false);
black = new BitmapFont(Gdx.files.internal("fonts/Black.fnt"), false);
black.setScale(Gdx.graphics.getWidth() / 1440f,
Gdx.graphics.getHeight() / 900f);
stage = new Stage();
atlas = new TextureAtlas("ui/Button.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("ButtonUp9");
textButtonStyle.down = skin.getDrawable("ButtonDown9");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = black;
exitButton = new TextButton("Exit", textButtonStyle);
exitButton.pad(10);
playButton = new TextButton("Play", textButtonStyle);
playButton.pad(10);
playButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new Scenarios());
Gdx.audio.newSound(Gdx.files.internal("sounds/button.mp3"))
.play(1);
Gdx.input.vibrate(50);
dispose();
}
});
exitButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
dispose();
}
});
table.add(playButton);
table.row();
table.add(exitButton);
table.getCell(exitButton).spaceTop(50f);
stage.addActor(table);
}
#Override
public void dispose() {
stage.dispose();
black.dispose();
white.dispose();
skin.dispose();
batch.dispose();
sprite.getTexture().dispose();
}
}
Screen3
public class Scenarios implements Screen {
private TextButtonStyle buttonStyle;
private TextButton[] scene;
private BitmapFont black;
private Stage stage;
private Table table;
private TextureAtlas atlas;
private Skin skin;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
Gdx.input.setInputProcessor(stage);
if (Gdx.input.isKeyPressed(Keys.BACKSPACE)) {
((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
} else if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
Gdx.app.exit();
}
}
#Override
public void show() {
atlas = new TextureAtlas("ui/Button.pack");
skin = new Skin(atlas);
stage = new Stage();
table = new Table(skin);
black = new BitmapFont(Gdx.files.internal("fonts/Black.fnt"), false);
black.setScale(Gdx.graphics.getWidth() / 1280f,
Gdx.graphics.getHeight() / 720f);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
buttonStyle = new TextButtonStyle();
buttonStyle.up = skin.getDrawable("ButtonUp9");
buttonStyle.down = skin.getDrawable("ButtonDown9");
buttonStyle.pressedOffsetX = 1;
buttonStyle.pressedOffsetY = -1;
buttonStyle.font = black;
scene = new TextButton[3];
scene[0] = new TextButton("Scenario 1", buttonStyle);
scene[1] = new TextButton("Scenario 2", buttonStyle);
scene[2] = new TextButton("Scenario 3", buttonStyle);
scene[0].addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new Intro1());
Gdx.audio.newSound(Gdx.files.internal("sounds/button.mp3"))
.play(1);
Gdx.input.vibrate(50);
dispose();
}
});
scene[1].addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new Intro2());
Gdx.audio.newSound(Gdx.files.internal("sounds/button.mp3"))
.play(1);
Gdx.input.vibrate(50);
dispose();
}
});
scene[2].addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
((Game) Gdx.app.getApplicationListener())
.setScreen(new Intro3());
Gdx.audio.newSound(Gdx.files.internal("sounds/button.mp3"))
.play(1);
Gdx.input.vibrate(50);
dispose();
}
});
scene[0].pad(10);
scene[1].pad(10f);
scene[2].pad(10f);
table.add(scene[0]);
table.add(scene[1]);
table.getCell(scene[1]).space(20f);
table.add(scene[2]);
stage.addActor(table);
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
atlas.dispose();
black.dispose();
}
}

You forgot to dispose sound/music objects. You may do it in dispose method of your Game class.

Related

Libgdx the correct way to make a game

Recently I started making my "first" real RPG (The other 'RPG's' were text based in batch or whatever). I am using Libgdx, since from what I read it's pretty simple to get the hang of.
But what I'm interested in is am I doing it right?
For example I make a TiledMap and draw the camera etc. everything 'semi works', but is it right? On one tutorial there was a person which created thread's and much more, so it's making me think I'm doing it wrong
For example this is my GameScreen
public class GameScreen implements Screen, Runnable{
private MenuScreen menuScreen;
SpriteBatch batch, infobatch;
BitmapFont font;
private int tileWidth, tileHeight,
mapWidthInTiles, mapHeightInTiles,
mapWidthInPixels, mapHeightInPixels;
private float delta;
private InputHandler inputHandler;
private TiledMap map;
private AssetManager manager;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
private FitViewport playerViewport;
private Player player;
public GameScreen(MenuScreen mc)
{
menuScreen=mc;
}
#Override
public void show() {
batch = new SpriteBatch();
infobatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.local("font/ornlan.fnt"));
font.setColor(Color.BLACK);
manager = new AssetManager();
manager.setLoader(TiledMap.class, new TmxMapLoader());
manager.load("world/test_1.tmx", TiledMap.class);
manager.finishLoading();
map = manager.get("world/test_1.tmx", TiledMap.class);
MapProperties properties = map.getProperties();
tileWidth = properties.get("tilewidth", Integer.class);
tileHeight = properties.get("tileheight", Integer.class);
mapWidthInTiles = properties.get("width", Integer.class);
mapHeightInTiles = properties.get("height", Integer.class);
mapWidthInPixels = mapWidthInTiles * tileWidth;
mapHeightInPixels = mapHeightInTiles * tileHeight;
camera = new OrthographicCamera(960.f, 720.f);
playerViewport = new FitViewport(mapWidthInTiles, mapHeightInTiles, camera);
camera.position.x = MathUtils.clamp(camera.position.x, camera.viewportWidth / 2, mapWidthInPixels - camera.viewportWidth /2);
camera.position.y = MathUtils.clamp(camera.position.y, camera.viewportHeight / 2, mapHeightInPixels - camera.viewportHeight / 2);
renderer = new OrthogonalTiledMapRenderer(map);
player = new Player();
inputHandler = new InputHandler();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.75f, 0.75f, 0.85f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
camera.position.set(Player.x + Player.width / 2, Player.y + Player.height / 2, 0);
camera.update();
renderer.setView(camera);
renderer.render();
infobatch.begin();
font.draw(infobatch, "Test World, Dev. purposes", 10,25);
font.draw(infobatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 1180, 700);
infobatch.end();
batch.begin();
batch.setProjectionMatrix(camera.combined);
delta = Gdx.graphics.getDeltaTime();
inputHandler.update();
player.update(delta);
player.render(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
batch.dispose();
infobatch.dispose();
font.dispose();
renderer.dispose();
manager.dispose();
}
#Override
public void run() {
// TODO Auto-generated method stub
}
}
And this is my "MainMenu"
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class MenuScreen extends Game {
SpriteBatch batch;
BitmapFont font;
private Stage stage;
private GameScreen gameScreen;
private Texture btn_start;
private TextureRegion btn_startRegion;
private TextureRegionDrawable btn_startDrawable;
private ImageButton btn_startIB;
#Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.local("font/ornlan.fnt"));
font.setColor(Color.BLACK);
btn_start = new Texture(Gdx.files.internal("ui/start_btn.png"));
btn_startRegion = new TextureRegion(btn_start);
btn_startDrawable = new TextureRegionDrawable(btn_startRegion);
btn_startIB = new ImageButton(btn_startDrawable);
btn_startIB.setPosition(10, 250);
stage = new Stage(new ScreenViewport());
stage.addActor(btn_startIB);
Gdx.input.setInputProcessor(stage);
btn_startIB.addCaptureListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
try {
System.out.println("Starting GameScreen...");
setGameScreen();
} catch (Exception e) {
System.out.println("Error starting GameScreen E:" + e);
}
};
});
}
#Override
public void render() {
Gdx.gl.glClearColor(0.67f, 0.75f, 0.98f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
font.getData().setScale(0.8f,0.8f);
font.draw(batch, "Ornlan DevBuild (pa) v0.0.0.1", 1000, 25);
batch.end();
stage.act(Gdx.graphics.getDeltaTime()); // Perform ui logic
stage.draw();
super.render();
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
super.dispose();
}
void setGameScreen() {
gameScreen = new GameScreen(this);
setScreen(gameScreen);
}
}

LibGdx: Shaperenderer Rect not being drawn on Screen

Trying to create a simple loading Screen. The below code prints the correct progress, so I know that part works. But the rectangle is not being drawn. Not sure what is wrong.
Full LoadingScreen:
public class LoadingScreen implements Screen {
private static final float PROGRESS_BAR_WIDTH = MyGdxGame.WIDTH / 2f;
private static final float PROGRESS_BAR_HEIGHT = 50f;
GdxAssetManager assetManager;
Stage stage;
//Table mainTable;
private ShapeRenderer shapeRenderer;
private MyGdxGame game;
public LoadingScreen(MyGdxGame game){
this.game = game;
assetManager = game.getAssetManager();
shapeRenderer = new ShapeRenderer();
stage = new Stage(new StretchViewport(MyGdxGame.WIDTH, MyGdxGame.HEIGHT));
}
#Override
public void show() {
assetManager.loadGeneral();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderProgressBar();
if (assetManager.getManager().update()) {
game.setScreen(new LoginScreen(game));
}
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
private void renderProgressBar() {
float progress = assetManager.getManager().getProgress();
System.out.println(PROGRESS_BAR_WIDTH * progress);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(
(MyGdxGame.WIDTH - PROGRESS_BAR_WIDTH) / 2f,
(MyGdxGame.HEIGHT - PROGRESS_BAR_HEIGHT) / 2f,
PROGRESS_BAR_WIDTH * progress,
PROGRESS_BAR_HEIGHT
);
shapeRenderer.end();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
shapeRenderer.dispose();
}
}
I guess methods of interest are render and renderProgressBar. Like I said, all I get is a white background until the loading is finished, but the print inside renderProgressBar prints the correct values.
Set projectionMatrix of ShapeRenderer using stage camera.
shapeRenderer.setProjectionMatrix(stage.getCamera().combined);

libgdx - stage was disabled when back button pressed

I'm using libgdx 1.9.0 for game development.
I have one stage in main screen that includes multiple buttons.
I have another screen code that shows game objects.
when the back button in android device is pressed the stage in main screen gets disabled
and listener functions for buttons don't work.
In Main Screen I have :
public class MainMenuScreen extends ScreenAdapter {
TextButton button_start;
TextButtonStyle textButtonStyle;
BitmapFont font;
FitViewport viewp;
Stage stage;
public MainMenuScreen(IndependentArch game) {
this.game = game;
/* add GUI controls */
stage = new Stage(new StretchViewport(game.SCREEN_WIDTH,
game.SCREEN_Height), game.batcher);
Gdx.input.setInputProcessor(stage);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = Assets.font_btitr;
textButtonStyle.up = Assets.skin.getDrawable("button");
textButtonStyle.down = Assets.skin.getDrawable("button-down");
button_start = new TextButton(Reshape.doReshape_mirror("start"),
textButtonStyle);
button_start.setBounds(0, 0, 200, 50);
button_start.setPosition(140, 255);
button_about.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
MyTextInputListener listener = new MyTextInputListener();
Gdx.input.getTextInput(listener, "name", "",
"please enter your name.");
}
});
stage.addActor(button_start);
}
public void update() {
if (Gdx.input.justTouched()) {
stage.getCamera().unproject(
touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
}
}
public void draw(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batcher.disableBlending();
game.batcher.begin();
game.batcher.draw(Assets.backgroundRegion[0], 0, 0, game.SCREEN_WIDTH,
game.SCREEN_Height);
game.batcher.end();
stage.act(delta);
stage.draw();
game.batcher.enableBlending();
game.batcher.begin();
game.batcher.draw(Assets.uiskin.findRegion("music"),
game.SCREEN_WIDTH - 90, 10, 80, 80);
game.batcher.end();
}
#Override
public void render(float delta) {
update();
draw(delta);
}}
and in Game Screen I have :
public class GameScreen implements Screen, InputProcessor {
OrthographicCamera guiCam;
Vector3 touchPoint;
World world;
WorldRenderer renderer;
CameraController camController;
InputMultiplexer multiplexer;
GestureDetector gestureDetector;
/** for debug rendering **/
ShapeRenderer debugRenderer = new ShapeRenderer();
int lastScore;
String scoreString;
float totalGameTime;
GlyphLayout glyphLayout = new GlyphLayout();
public GameScreen(IndependentArch game) {
this.game = game;
Gdx.input.setCatchBackKey(true);
state = GAME_READY;
guiCam = new OrthographicCamera(game.SCREEN_WIDTH, game.SCREEN_Height);
guiCam.position.set(game.SCREEN_WIDTH / 2, game.SCREEN_Height / 2, 0);
touchPoint = new Vector3();
world = new World();
renderer = new WorldRenderer(game.batcher, world);
Gdx.input.setInputProcessor(this);
camController = new CameraController(world);
multiplexer = new InputMultiplexer();
multiplexer.addProcessor(new GestureDetector(20, 0.5f, 2, 0.15f,
camController));
multiplexer.addProcessor(this);
Gdx.input.setInputProcessor(multiplexer);
}
// * InputProcessor methods ***************************//
#Override
public boolean keyDown(int keycode) {
if (keycode == Keys.BACK) {
game.setScreen(new MainMenuScreen(game));
// Gdx.app.exit();
}
return false;
}}
Override the show() method in the main menu screen and set the input processor to stage there
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}

libgdx click area above target

I'm trying to make a simple main menu with clickable play and exit labels, but clickable area is always above labels. To click "play" label I need to click about 20 pixels above it.
How can I align clickable area with labels?
There is my MainMenuButton class
public class MainMenuButtons {
private MainGame game;
private Stage stage;
private Viewport gameViewport;
private Label mainGameLabel, playLabel, exitLabel;
public MainMenuButtons(MainGame game) {
this.game = game;
gameViewport = new FillViewport(GameInfo.WIDTH, GameInfo.HEIGHT, new OrthographicCamera());
stage = new Stage(gameViewport, game.getBatch());
Gdx.input.setInputProcessor(stage);
createLabelsAndButtons();
addAllListeners();
Table menuTable = new Table();
menuTable.center().center();
menuTable.setFillParent(true);
menuTable.add(playLabel);
menuTable.row();
menuTable.add(exitLabel).padTop(40);
Table gameTitle = new Table();
gameTitle.center().top();
gameTitle.setFillParent(true);
gameTitle.add(mainGameLabel).padTop(40);
menuTable.debug();
stage.addActor(menuTable);
stage.addActor(gameTitle);
}
void createLabelsAndButtons() {
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Fonts/PressStart2P.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 45;
BitmapFont font = generator.generateFont(parameter);
mainGameLabel = new Label("Road Racer", new Label.LabelStyle(font, Color.WHITE));
parameter.size = 25;
font = generator.generateFont(parameter);
playLabel = new Label("Play", new Label.LabelStyle(font, Color.WHITE));
exitLabel = new Label("Exit", new Label.LabelStyle(font, Color.WHITE));
}
void addAllListeners() {
playLabel.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
System.out.println("Play label clicked " + y);
// game.setScreen(new GamePlay(game));
}
});
exitLabel.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
System.out.println("Exit label clicked " + y);
// Gdx.app.exit();
}
});
}
public Stage getStage() {
return this.stage;
}
}
And I call it in the MainMenu class
/**
* Created by vladk on 24/09/16.
*/
public class MainMenu implements Screen {
private MainGame game;
private OrthographicCamera mainCamera;
private Viewport gameViewport;
private MainMenuButtons btns;
public MainMenu(MainGame game) {
this.game = game;
mainCamera = new OrthographicCamera();
mainCamera.setToOrtho(false, GameInfo.WIDTH, GameInfo.HEIGHT);
mainCamera.position.set(GameInfo.WIDTH / 2f, GameInfo.HEIGHT / 2f, 0);
gameViewport = new StretchViewport(GameInfo.WIDTH, GameInfo.HEIGHT, mainCamera);
// background ??
// buttons
btns = new MainMenuButtons(game);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// game.getBatch().begin();
// game.getBatch().end();
game.getBatch().setProjectionMatrix(btns.getStage().getCamera().combined);
btns.getStage().draw();
btns.getStage().act();
}
#Override
public void resize(int width, int height) {
gameViewport.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
// bg
// buttons
btns.getStage().dispose();
}
}
I found a solution. I needed to update view port for the MainMenuButtons, so I created a new method
// in the MainMenuButtons class
public void viewportUpdate(int width, int height) {
gameViewport.update(width, height);
}
and call if from the MainMenu class
#Override
public void resize(int width, int height) {
gameViewport.update(width, height);
btns.viewportUpdate(width, height);
}

LibGDX Screens overlapping when changing screens

Hi i'm kind of new to libGDX and I'm just creating a simple game app. I have a main menu screen and a level screen for my game. I have initialised the startup screen to the main menu. When i click the 'Play' button in the main menu screen, I want to switch the screen to the level select screen but what happens is that it does change the screen but the old one is rendering behind that as well. This is what is happening when i click the play button: http://prntscr.com/57teri
Game class:
public class BallJump extends Game {
MainMenu menu;
LevelSelect levelSelect;
#Override
public void create () {
menu = new MainMenu(this);
levelSelect = new LevelSelect(this);
setScreen(menu);
}
#Override
public void render () {
super.render();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void dispose() {
super.dispose();
}
}
Main menu screen:
public class MainMenu implements Screen{
BallJump game;
Stage stage;
GameButton playButton, exitButton;
Texture title;
Sprite test;
SpriteBatch batch;
OrthographicCamera camera;
public MainMenu(BallJump game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
System.out.println("main menu");
stage.act();
stage.draw();
batch.begin();
test.draw(batch);
batch.end();
}
#Override
public void show() {
camera = new OrthographicCamera();
title = new Texture(Gdx.files.internal("images/title.png"));
test = new Sprite(title);
batch = new SpriteBatch();
stage = new Stage();
test.setPosition(Gdx.graphics.getWidth()/7f, Gdx.graphics.getHeight()/1.75f);
playButton = new GameButton("PLAY");
playButton.setPosition(Gdx.graphics.getWidth()/2.5f, Gdx.graphics.getHeight()/2);
playButton.setHeight(50f);
playButton.setWidth(150f);
playButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(game.levelSelect);
dispose();
return true;
}});
stage.addActor(playButton);
exitButton = new GameButton("EXIT");
exitButton.setPosition(Gdx.graphics.getWidth()/2.5f, Gdx.graphics.getHeight()/3);
exitButton.setHeight(50f);
exitButton.setWidth(150f);
exitButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}});
stage.addActor(exitButton);
Gdx.input.setInputProcessor(stage);
}
#Override
public void hide() {
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
#Override
public void dispose() {
batch.dispose();
stage.clear();
stage.dispose();
title.dispose();
}
Level select screen :
public class LevelSelect implements Screen{
BallJump game;
Stage stage;
TextButton l1, buttonExit;
Texture title;
Sprite test;
SpriteBatch batch;
OrthographicCamera camera;
public LevelSelect(BallJump game) {
this.game = game;
}
#Override
public void show() {
batch = new SpriteBatch();
stage = new Stage();
title = new Texture(Gdx.files.internal("images/level_select.png"));
test = new Sprite(title);
test.setPosition(Gdx.graphics.getWidth()/7f, Gdx.graphics.getHeight()/1.75f);
stage.addActor(l1);
}
#Override
public void render(float delta) {
stage.act();
stage.draw();
batch.begin();
test.draw(batch);
batch.end();
System.out.println("level select");
}
Your LevelSelect screen needs to clear the buffer before drawing. If you don't do it, the last rendered frame from the Menu will always be on the buffer.
#Override
public void render(float delta) {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
...
}

Categories