LibGDX - How to clear the Screen - java

I'm trying to display 2 different screens, changing when the user touches the screen. So far with the code below the screens change but the text just keeps overlapping and piling up. I need to dispose of EVERYTHING on the screen before switching.
One of the 2 similar pages(only the text is different on the 2)
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
public class MainMenu implements Screen {
OrthographicCamera camera;
SpriteBatch batch;
Screens game;
BitmapFont font;
public MainMenu(Screens game) {
this.game = game;
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void render(float delta) {
CharSequence str = "Main Menu";
batch = new SpriteBatch();
font = new BitmapFont();
batch.begin();
font.draw(batch, str, 200, 200);
batch.end();
if (Gdx.input.justTouched()) // use your own criterion here
game.setScreen(game.anotherScreen);
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}
Screens.java
package com.me.mygdxgame;
import com.badlogic.gdx.Game;
public class Screens extends Game {
MainMenu mainMenuScreen;
AnotherScreen anotherScreen;
#Override
public void create() {
mainMenuScreen = new MainMenu(this);
anotherScreen = new AnotherScreen(this);
setScreen(mainMenuScreen);
}
}

Change your render function to:
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //clears the buffer
CharSequence str = "Main Menu";
batch = new SpriteBatch();
font = new BitmapFont();
batch.begin();
font.draw(batch, str, 200, 200);
batch.end();
if (Gdx.input.justTouched()) // use your own criterion here
game.setScreen(game.anotherScreen);
}

Related

How to not resize but move widgets when screen resized?

I have added buttons to my code, but when I go to "resize", they get bigger or wider and it looks very bad. How can I resize the window and keep the button aspect ratio?
This is my code:
1)Main:
`package com.mygdx.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.game.screen.MainMenuScreen;
public class gamename extends Game {
public static final int WIDTH = 760;
public static final int HEIGHT = 520;
public SpriteBatch batch;
#Override
public void create() {
batch = new SpriteBatch();
this.setScreen(new MainMenuScreen(this));
}
#Override
public void render(){
super.render();
}
}`
2)And this is the menu, here i use the image to view the buttons. MainMenu:
package com.mygdx.game.screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.mygdx.game.gamename;
public class MainMenuScreen implements Screen{
private static final int PLAY_BUTTON_WIDTH = 150;
private static final int PLAY_BUTTON_HEIGHT = 80;
private static final int OPTIONS_BUTTON_WIDTH = 150;
private static final int OPTIONS_BUTTON_HEIGHT = 80;
gamename game;
Texture playButtonActive;
Texture playButtonInactive;
Texture optionsButtonActive;
Texture optionsButtonInactive;
public MainMenuScreen (gamename game){
this.game = game;
playButtonActive = new Texture("play1.png");
playButtonInactive = new Texture("play2.png");
optionsButtonActive = new Texture("options1.png");
optionsButtonInactive = new Texture("options2.png");
}
#Override
public void show() {
}
#Override
public void render(float f) { //Colore e carica bottoni
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.batch.draw(playButtonActive, 240, 150, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT);
game.batch.end();
}
#Override
public void resize(int i, int i1) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
I would like the button to be the same even if I manually increase the page size. Some say they use viewport but I have never used it and I don't know how to do it. Can someone help me?

how to do Junit test for a Libgdx class has pictures and pictures button

the Menu Class
package com.gdx.game.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.gdx.game.HuntersGame;
public class InfoScreen implements Screen {
HuntersGame game;
Texture background,backBefore,backAfter,textInfo;
/**
* Constructor, Links the Game instance
* #param game the Game instance provided by the GameScreen, contains the sprite batch
* which is essential for rendering
*/
public InfoScreen(HuntersGame game){
this.game = game;
//play button as image
backBefore = new Texture("backBefore.png");
backAfter = new Texture("backAfter.png");
//text
textInfo = new Texture("info.png");
//background
background = new Texture("grass-info.jpg");
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//positions of back button
int xb= backBefore.getWidth();
int yb= backBefore.getHeight();
//positions of the mouse
int x=Gdx.input.getX(),y=Gdx.input.getY();
//start batch
game.batch.begin();
game.batch.draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
game.batch.draw(textInfo,0,-30,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
//start script for touch and click by mouse
if(x>=0 && x<=backBefore.getWidth() && y>=0 && y<=backBefore.getHeight()){
game.batch.draw(backAfter,0,Gdx.graphics.getHeight()-backBefore.getHeight());
if(Gdx.input.isTouched()){
this.dispose();
game.setScreen(new MenuScreen(game));
}
}else{
game.batch.draw(backBefore,0,Gdx.graphics.getHeight()-backBefore.getHeight());
}
game.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() {
}
}
I wanted to do a test for this class I don't know how and I tried to import Mockito as every other code did but not work with me I tried for like 6 hours with no result cause I don't know how that Mockito works and search too much with no benefits and I want just an example for that class then I will try to do advanced examples
thank you in advance
PS: for the images i used you can use any image to check the code and for HunterGame class is down
package com.gdx.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.gdx.game.screens.GameScreen;
import com.gdx.game.screens.MenuScreen;
public class HuntersGame extends Game {
public SpriteBatch batch;
#Override
public void create () {
batch = new SpriteBatch();
this.setScreen(new MenuScreen((this)));
}
#Override
public void dispose () {
batch.dispose();
}
}
[the info class looks like][1]

LibGDX Scene2d.ui shows only white screen with the following code

package com.Darth377Apps.DeepShadow;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.utils.viewport.*;
public class MainMenu implements Screen
{
private Stage stage;
private Table table;
private TextureAtlas atlas;
private TextButton buttonPlay, buttonExit;
private Label heading;
private BitmapFont white;
private Skin skin;
private TextButton TextButton;
#Override
public void create(){
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(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());
white= new BitmapFont(Gdx.files.internal("Font/white.fnt"),false);
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("button.up.9");
textButtonStyle.down = skin.getDrawable("button.down.9");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
textButtonStyle.fontColor = Color.BLACK;
buttonExit = new TextButton("EXIT",textButtonStyle);
buttonExit.pad(20);
table.add(buttonExit);
stage.addActor(table);
}
#Override
public void render(float p1)
{
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(p1);
stage.draw();
}
#Override
public void resize(int p1, int p2)
{
// TODO: Implement this method
}
#Override
public void show(){
}
#Override
public void hide()
{
}
#Override
public void pause()
{
// TODO: Implement this method
}
#Override
public void resume()
{
// TODO: Implement this method
}
#Override
public void dispose()
{
// TODO: Implement this method
}
}
When I run the app, only a white screen appears. The app definitely runs this code, as I ha e tested through logcat.
I am extremely confused as to why the text button does not appear. Any help would be greatly appreciated. Thank you.
Try to not set the bounds of the Table, but to use table.setFillParent(true) instead. Furthermore you need to implement resize(...) in the following way:
public void resize(int p1, int p2)
{
stage.getViewport().update(p1, p2, true);
}

LibGDX - My splash screen isn't showing

Here is my splash screen:
package com.badlogic.gdx.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
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 Splash implements Screen {
private SpriteBatch batch;
private Sprite splash;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1); //sets clear color to black
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch
batch.begin();
splash.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("badlogic.jpg"));
splash = new Sprite(texture);
splash.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
When I run the application, the splash screen doesn't show, it just jumps straight to the game itself. I'm knew at this so I'm not completely sure what the problem is. My best guess is that the splash screen isn't linked to the game screen but I'm not sure.
Heres the Main Game screen, I don't have a menu yet.
public class SlingshotSteve extends Game {
private OrthographicCamera camera;
// Creates our 2D images
private SpriteBatch batch;
private TextureRegion backgroundTexture;
private Texture texture;
#Override
public void create() {
setScreen(new Splash());
camera = new OrthographicCamera(1280, 720);
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);
Music mp3Sound = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
mp3Sound.setLooping(true);
mp3Sound.play();
}
#Override
public void render() {
super.render();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
super.dispose();
}
}
I believe your problem lies here
#Override
public void render() {
super.render();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
I believe that super.render() calls your current screen's render method, which means that your splash screen is indeed drawing itself. However, since right afterwards you draw your background, the actual splash screen is not visible.
Try commenting out everything but super.render(); in your render method, and see if that doesn't fix things.
It's because you provide an implementation for render() in your Game class, rather than use a separate Screen. If you use Screens, you mustn't provide an implementation for render() in your Game class.
Also, you should allocate the Texture for the SplashScreen in a constructor, rather than in the show() method. But that's just a minor caveat.

libgdx my splashscreen is not showing

I'm writing an libgdx game, but it seems that i cannot show my splashscreen, it only shows a black screen. does anyone know how this comes?
I don't get any runtime error, it only shows a blackscreen, even if i set the glClearColor to an other color then black
package mygame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class SplashScreen implements Screen {
Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
splashTexture = new Texture("mygame/splash.png");
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite = new Sprite(splashTexture);
//splashSprite.setColor(1, 1, 1, 0);
splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));
batch = new SpriteBatch();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
I tried your code (see below) and it works well enough, so perhaps the problem lies in the MyGame class. Are you, for example, overriding Game.render() then not calling super.render()?
package hacks;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGame extends com.badlogic.gdx.Game {
#Override
public void create() {
setScreen(new SplashScreen(this));
}
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = MyGame.class.getName();
config.width = 800;
config.height = 480;
config.fullscreen = false;
config.useGL20 = true;
config.useCPUSynch = true;
config.forceExit = true;
config.vSyncEnabled = true;
new LwjglApplication(new MyGame(), config);
}
}
class SplashScreen implements Screen {
Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
splashTexture = new Texture("assets/data/libgdx.png");
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite = new Sprite(splashTexture);
// splashSprite.setColor(1, 1, 1, 0);
splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));
batch = new SpriteBatch();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
this is how my game class looks like:
package mygame;
import com.badlogic.gdx.Game;
public class MyGame extends Game {
#Override
public void create() {
setScreen(new SplashScreen(this));
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void render() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
This is how your game class should look like...you need to call the super functions .
package com.sample;
import com.badlogic.gdx.Game;
public class Sample extends Game{
#Override
public void dispose() {
// TODO Auto-generated method stub
super.dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
super.resume();
}
#Override
public void render() {
// TODO Auto-generated method stub
super.render();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
super.resize(width, height);
}
#Override
public void create() {
// TODO Auto-generated method stub
setScreen(new AnotherScreen(this));
}
}

Categories