LIBGDX TextButton - java

I'm unsuccessfully trying to add a button on my interface with the default skin. (http://download.bitowl.de/defaultskin.zip)
My code :
public class Core extends ApplicationAdapter {
Stage stage;
SpriteBatch batch;
Texture background;
TextButton button;
Skin skin;
OrthographicCamera camera;
Vector3 mouse;
#Override
public void create() {
camera = new OrthographicCamera();
batch = new SpriteBatch();
stage = new Stage();
mouse = new Vector3();
batch.setProjectionMatrix(camera.combined);
background = new Texture("fondLogoTikal.jpg");
skin = new Skin(Gdx.files.internal("ui/defaultskin.json"));
TextButton button = new TextButton("Mon boutton", skin);
button.setPosition(125, 200);
stage.addActor(button);
}
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
}
#Override
public void render() {
batch.begin();
batch.setProjectionMatrix(camera.combined);
batch.draw(background, 0, 0);
batch.end();
}
#Override
public void dispose() {
batch.dispose();
stage.dispose();
background.dispose();
}
}
My error :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: ui/defaultskin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:98)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:75)
at com.mygdx.game.Core.create(Core.java:32)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
however, I correctly put the ui file in the assets file.
And if I delete the TextButton, my code works correctly

Related

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 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);
...
}

Why is this not displaying my background image title.png?

apologies in advance for the very newbie question, currently trying to learn libGDX, every tutorial suggests this should work, its correctly displaying the text button in the centre of the screen however it isnt showing the background image title.png.
ive played around with it quite a bit to try and get it to work with no luck, any help you can give me would be greatly appreciated!
public class MainMenu implements Screen {
ZebraGems game;
Stage stage;
BitmapFont font;
BitmapFont blackfont;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextButton button;
Texture titleTexture;
Sprite titleSprite;
public MainMenu(ZebraGems game){
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
batch.begin();
titleSprite.draw(batch);
stage.draw();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null);
stage = new Stage (width, height, true);
stage.clear();
titleTexture = new Texture("data/title.png");
titleSprite = new Sprite(titleTexture);
titleSprite.setColor(1,1,1,0);
titleSprite.setSize(480, 800);
titleSprite.setPosition (0, 0);
Gdx.input.setInputProcessor(stage);
TextButtonStyle style = new TextButtonStyle();
style.up = skin.getDrawable("play");
style.down = skin.getDrawable("playpressed");
style.font = blackfont;
button = new TextButton("", style);
button.setWidth(213);
button.setHeight(94);
button.setX(Gdx.graphics.getWidth() /2 - button.getWidth() /2);
button.setY(Gdx.graphics.getHeight() /2 - button.getHeight() /2);
stage.addActor(button);
}
#Override
public void show() {
batch = new SpriteBatch();
atlas = new TextureAtlas("data/playButton.pack");
skin = new Skin();
skin.addRegions(atlas);
blackfont = new BitmapFont(Gdx.files.internal("data/blackfont.fnt"));
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
blackfont.dispose();
}
}
Move stage.draw() out of the batch.begin() and batch.end().
Furthermore in case you are using the latest nighty build of libgdx, you should add stage.getViewport().update(width, height, true) to your resize(...) method.
Furthermore you are doing this titleSprite.setColor(1,1,1,0) which sets the alpha of the background sprite to 0 which means 100% transparent = invisible.
Do this instead: titleSprite.setColor(1,1,1,1).
In total your render should look like this:
batch.begin();
titleSprite.draw(batch);
batch.end();
stage.act(delta);
stage.draw();

LIBGDX how to add sprite from a texture atlas

I need to know how I can add a sprite from a texture atlas so I can implement it as a sprite for my main menu.
Here is my code for the main menu:
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextureRegion firstLayer;
TextureRegion secondLayer;
TextureRegion thirdLayer;
TextureRegion fourthLayer;
TextureRegion fifthLayer;
TextureRegion sixthLayer;
TextureRegion seventhLayer;
TextureRegion eighthLayer;
TextureRegion ninthLayer;
TextureRegion tenthLayer;
TextureRegion eleventhLayer;
public MainMenu(CrazyZombies game){
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
batch.begin();
batch.end();
}
#Override
public void resize(int width, int height) {
Gdx.input.setInputProcessor(stage);
}
#Override
public void show() {
batch = new SpriteBatch();
skin = new Skin();
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
}
public void loadLayers() {
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/mainmenu/MainMenu.pack"));
firstLayer = atlas.findRegion("1layer");
}
}
So I need to go about getting firstLayer onto the screen and every way I have tried ended with a crash.
You are not doing it right. Your render method doesn't have any draw. Take a look at the SuperJumper demo in libgdx.
To answer on how to create a sprite-
Sprite mySprite;
...
...
mySprite = new Sprite(someTextureRegion);
mySprite.setSize(SOME_WIDTH, SOME_HEIGHT);
mySprite.setOrigin(SOME_WIDTH/2f, SOME_HEIGHT/2f);
mySprite.setPosition(world.sprite.bounds.x, world.sprite.bounds.y);
...
...
mySprite.draw(batch);

libgdx with scene2d and actor is not displaying the sprite

I am testing out Libgdx and Scene2d. I expected this small program to display a logo, but it draws a black screen only. Any idea what am I missing?
public class MyGame implements ApplicationListener {
private Stage stage;
#Override
public void create() {
stage = new Stage(800, 800, false);
Gdx.input.setInputProcessor(stage);
MyActor actor = new MyActor();
stage.addActor(actor);
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void resize(int width, int height) {
stage.setViewport(800, 800, false);
}
}
public class MyActor extends Actor {
Sprite sprite;
public MyActor() {
sprite = new Sprite();
sprite.setTexture(new Texture("data/libgdx.png"));
setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());
setTouchable(Touchable.enabled);
setX(0);
setY(0);
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(sprite, getX(), getY());
}
}
Construct the sprite with the texture and use Gdx.file.internal:
sprite = new Sprite(new Texture(Gdx.files.internal("data/libgdx.png")));
Anyway, if you just want to display and act on images, you might prefer to use Image class:
private Stage stage;
private Texture texture;
#Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
com.badlogic.gdx.scenes.scene2d.ui.Image actor = new com.badlogic.gdx.scenes.scene2d.ui.Image(region);
stage.addActor(actor);
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
I was getting a black screen too until I explicitly set Actor's height (setHeight(height)) and width (setWidth(width)) to the Sprite's values.
tex = new Texture(Gdx.files.internal("happy.png"));
Image happy = new Image(tex);
/* happy.setBounds(happy.getX(), happy.getY(), happy.getWidth(), happy.getHeight()); not needed if using full image */
stage.addActor(happy);
Your problem is most likely this line, in the draw method
batch.draw(sprite, getX(), getY());
The code I have seen when drawing sprites is
sprite.draw(batch);

Categories