I create the menu button in the table.
I did not point anywhere in the border the table but it is displayed.
I ran in the emulator or in desktop mode border appears
maybe I create the wrong table?
#Override
public void show() {
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//Button PLAY
//скин кнопки плай
btnplayStyle = new ImageButton.ImageButtonStyle();
btnplayStyle.up = buttonsSkin.getDrawable("play");//кнопка не нажата
btnplayStyle.over = buttonsSkin.getDrawable("play");
btnplayStyle.down = buttonsSkin.getDrawable("play"); // кнопка нажата
playSkin = new ImageButton(btnplayStyle);
playSkin.setSize(300, 200);
stage.addActor(playSkin);
playSkin.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new GameScreen(game));
}
});
//Button score
btnscoreStyle = new ImageButton.ImageButtonStyle();
btnscoreStyle.up = buttonsSkin.getDrawable("records");//кнопка не нажата
btnscoreStyle.over = buttonsSkin.getDrawable("records");
btnscoreStyle.down = buttonsSkin.getDrawable("records"); // кнопка нажата
scorebutton = new ImageButton(btnscoreStyle);
scorebutton.setSize(300, 200);
stage.addActor(scorebutton);
scorebutton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new Score(game) {
});
}
});
//Button EXIT
btnexitStyle = new ImageButton.ImageButtonStyle();
btnexitStyle.up = buttonsSkin.getDrawable("exit");//кнопка не нажата
btnexitStyle.over = buttonsSkin.getDrawable("exit");
btnexitStyle.down = buttonsSkin.getDrawable("exit"); // кнопка нажата
exxitbutton = new ImageButton(btnexitStyle);
exxitbutton.setSize(300, 200);
stage.addActor(exxitbutton);
exxitbutton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
// table.add(heading);
table.add(playSkin).width(400).height(100);
table.getCell(playSkin).spaceBottom(30);
table.row();
table.add(scorebutton).width(400).height(100);
table.getCell(scorebutton).spaceBottom(30);
table.row();
table.add(exxitbutton).width(400).height(100);
table.getCell(exxitbutton).spaceBottom(30);
table.row();
table.debug();
stage.addActor(table);
}
After the start button will work properly, in the middle, but the border on each side as well created en understand because of what
The problem is that at the very end of your code you have :
table.debug();
Which according to the LibGDX Scene2d.ui documentation
(https://github.com/libgdx/libgdx/wiki/Scene2d.ui)
"enables debug lines for tables"
So, simply just remove that line of code and it should work!
Related
Ive been playing around with libgdx for the past couple of days and I wanted to see if someone could give a simple example of how a main menu screen is created with a start button and exit button.
LibGDX suggests to use its "scene2d" component if you need to build a UI.
The simplest example can be found on LibGDX's Wiki right in the article about scene2d:
https://libgdx.com/wiki/graphics/2d/scene2d/scene2d
Actually, the first code example from the wiki will do - it is a bit modified by me and shoud be added to your implementation of Screen or Game:
private Stage stage;
#Override
public void create () {
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
// Insert your UI elements here, for example:
Skin skin = new Skin(Gdx.files.internal("skin.json")); // https://libgdx.com/wiki/graphics/2d/scene2d/skin
Table menuContainer = new Table();
TextButton playButton = new TextButton("Play", skin);
playButton.addListener(new ClickListener() {
#Override
public void clicked (InputEvent event, float x, float y) {
// Called when player clicks on Play button
}
});
menuContainer.add(playButton);
TextButton exitButton = new TextButton("Exit", skin);
exitButton.addListener(new ClickListener() {
#Override
public void clicked (InputEvent event, float x, float y) {
// Called when player clicks on Exit button
}
});
menuContainer.add(exitButton);
}
#Override
public void resize (int width, int height) {
// See below for what true means.
stage.getViewport().update(width, height, true);
}
#Override
public void render () {
float delta = Gdx.graphics.getDeltaTime();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void dispose () {
stage.dispose();
}
when i run emulator and start application i see blinks screen, but on a PC in DesktopLauncher screen is normal.
I can not understand where I made mistakes.
render method override but it does not help.
class starter:
public class Drop extends Game {
Camera cam;
Game game;
SpriteBatch batch;
int tempGameScore = 0;
int dropsGatchered = 0;
Preferences preferences;//сохраняем игру
#Override
public void create() {
batch = new SpriteBatch();
this.setScreen(new MainMenuScreen(this));
}
#Override
public void render() {
super.render();
}
#Override
public void dispose() {
batch.dispose();
}
MainMenu:
I think I'm working wrong with the scene tool.
Can it is necessary as otherwise to draw a scene?
public class MainMenuScreen implements Screen {
Sprite texture;
final Drop game;
Skin skin;
Stage stage;
OrthographicCamera camera;
ImageButton newGameButton, exit, highScore;
Table table;
ImageButton.ImageButtonStyle btnplayStyle, btnscoreStyle, btnexitStyle;
private static TextureAtlas atlas, backAtlas;
public MainMenuScreen(final Drop gam) {
this.game = gam;
atlas = new TextureAtlas(Gdx.files.internal("texture/texture.pack"), true);
backAtlas = new TextureAtlas(Gdx.files.internal("texture/background.pack"), true);
texture = new Sprite(backAtlas.findRegion("background"));
skin = new Skin();
skin.addRegions(atlas);
table = new Table();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
stage = new Stage();
stage.addActor(table);
Gdx.input.setInputProcessor(stage);// Make the stage consume events
table();
}
private void table() {
btnplayStyle = new ImageButton.ImageButtonStyle();
btnplayStyle.up = skin.getDrawable("play");//кнопка не нажата
btnplayStyle.over = skin.getDrawable("play");
btnplayStyle.down = skin.getDrawable("play"); // кнопка нажата
newGameButton = new ImageButton(btnplayStyle);
newGameButton.setSize(300, 200);
stage.addActor(newGameButton);
newGameButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new GameScreen(game));
}
});
//Button score
btnscoreStyle = new ImageButton.ImageButtonStyle();
btnscoreStyle.up = skin.getDrawable("records");//кнопка не нажата
btnscoreStyle.over = skin.getDrawable("records");
btnscoreStyle.down = skin.getDrawable("records"); // кнопка нажата
highScore = new ImageButton(btnscoreStyle);
highScore.setSize(300, 200);
stage.addActor(highScore);
highScore.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new Score(game) {
});
}
});
//Button EXIT
btnexitStyle = new ImageButton.ImageButtonStyle();
btnexitStyle.up = skin.getDrawable("exit");//кнопка не нажата
btnexitStyle.over = skin.getDrawable("exit");
btnexitStyle.down = skin.getDrawable("exit"); // кнопка нажата
exit = new ImageButton(btnexitStyle);
exit.setSize(300, 200);
stage.addActor(exit);
exit.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
game.batch.begin();
game.batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
table.add(newGameButton).width(400).height(120);
table.getCell(newGameButton).spaceBottom(30);
table.row();
table.add(highScore).width(400).height(120);
table.getCell(highScore).spaceBottom(30);
table.row();
table.add(exit).width(400).height(120);
table.getCell(exit).spaceBottom(30);
table.row();
game.batch.end();
}
#Override
public void render(float delta) {
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
I think I'm working wrong with the scene tool
In MainMenuScreen you have to draw things in the render() method, not in the show() method. Like this:
#Override
public void render() {
stage.draw();
// ... possibly more drawing code
}
public class GameScreen implements Screen {
public void show()
{
buttonsAtlas = new TextureAtlas("Color.pack"); //button atlas image
buttonSkin = new Skin();
buttonSkin.addRegions(buttonsAtlas);
font = new BitmapFont(Gdx.files.internal("CustomFont.fnt"), false); //the font
stage = new Stage(); // window is stage
stage.clear();
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle(); // button properties
style.up = buttonSkin.getDrawable("Red");
style.down = buttonSkin.getDrawable("Blue");
style.font = font;
gameButton = new TextButton("Game Screen", style); //button text and style
gameButton.setPosition(-250, 500); //button location
gameButton.setHeight(600); //button height
gameButton.setWidth(1200); //button width
shopButton = new TextButton("Shop Menuuuu", style); //button text and style
shopButton.setPosition(100, 500); //button location
shopButton.setHeight(600); //button height
shopButton.setWidth(1200); //button width
stage.addActor(shopButton);
stage.addActor(gameButton);
Gdx.input.setInputProcessor(stage);
gameButton.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
gameButton.setBounds(-250, 500, 100, 100);
Gdx.app.log("my app,", "pressed");
game.setScreen(new inGame(game));
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("my app", "released");
}
});
shopButton.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
shopButton.setBounds(100, 500, 100, 100);
Gdx.app.log("my app,", "pressed");
game.setScreen(new Shop(game));
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("my app", "released");
}
});
}
public void render(float x)
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
game.batch.begin();
stage.draw();
game.batch.end();
}
public void dispose()
{
batch.dispose();
buttonSkin.dispose();
buttonsAtlas.dispose();
stage.dispose();
}
I am trying to make two TextButtons to work at the same time. However there is always one button showing on the screen, which is the last button that was being added to the stage. In this case it is the gameButton because it is the last button that was added to the stage.
How do I make both button work?
There is no point in setting negative Coordinate, may be you need to understand Coordinate systems for game:
o,o is at bottom left corner of screen
so when you give negative value, that part is not being displayed.
so two images are overlapping in your case and stage gives z index to actors and object which is added last will have highest z index, that means last added object will be draws on top of others.
What you need to do:
gameButton.setPosition(0, 0); //x,y --absolute to bottom left corner
gameButton.setSize(200,200); //width, height
shopButton.setPosition(200, 0); //x,y --absolute to bottom left corner
shopButton.setSize(200,200); //width, height
also doing setbound inside actionlistner is not good idea
I am having issues with libgdx sound inside a ClickListener for my button.
I am getting the error Syntax error on token "PlayButtonSound", Identifier expected after this token
SoundManager:
import utils.Constants;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
public class SoundManager {
private static Sound buttonSound = Gdx.audio.newSound(Constants.buttonSound);
private static Music song = Gdx.audio.newMusic(Constants.song);
public static void PlayMusic() {
song.setLooping(true);
song.setVolume(0.2f);
song.play();
}
public static void PlayButtonSound() {
buttonSound.play();
}
public void DestryoAudio() {
buttonSound.dispose();
song.dispose();
}
}
And MainMenu:
public class MainMenu implements Screen {
private Stage stage;
private Sprite sprite;
private TextureRegion menuBackgroundImg;
private TextureAtlas menuButton;
private Skin skin;
private BitmapFont font;
private Table table;
private TextButton playButton;
#Override
public void show() {
// Set stage
stage = new Stage();
Gdx.input.setInputProcessor(stage);
SoundManager.PlayMusic();
// Set menu baggrund
menuBackgroundImg = new TextureRegion(new Texture(Gdx.files.internal(Constants.menuBackgroundImg)));
sprite = new Sprite(menuBackgroundImg);
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Set menuknapper
menuButton = new TextureAtlas(Constants.menuButton);
skin = new Skin(menuButton);
// Set font
font = new BitmapFont(Constants.font, false);
// Set table
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Create button styling
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("menuButtonUp");
textButtonStyle.down = skin.getDrawable("menuButtonPressed");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = font;
textButtonStyle.fontColor = Color.BLACK;
// Create buttons
playButton = new TextButton(Constants.play, textButtonStyle);
playButton.addListener(new InputListener() {
SoundManager.PlayButtonSound(); // This is the error
});
// Button padding
playButton.pad(20, 100, 20, 100);
// Setting the table
table.add(playButton).row();
// Setting stage actor
stage.addActor(table);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Tegn baggrund
stage.getBatch().begin();
sprite.draw(stage.getBatch());
stage.getBatch().end();
// Tegn resten
stage.act(delta);
stage.draw();
}
...
#Override
public void dispose() {
stage.dispose();
menuButton.dispose();
skin.dispose();
font.dispose();
}
}
I can't really grasp what I am doing wrong and a search for the error gives vague answers that dosen't really solves my issue.
P.S. I have imported SoundManager but left it out due to length of the code snippet.
You need to implement one of the InputListener interface methods, most likely touchDown(InputEvent event, float x, float y, int pointer, int button).
Check out the API for InputListener. It lists all the methods and gives a pretty good example.
playButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
SoundManager.PlayButtonSound();
return false;
}
});
I'm writing a new program in LibGDX handling Backgroundtextures and have just begun to implement the screens. But when I test it, it just shows me a black cleared screen with the given resolution.
I call the screen with the setScreen(screen)- Method in an implemented Game class.
So here is the code:
public class MenuScreen implements Screen{
Table table;
Stage stage;
TextButton button;
TextField textField;
Texture texture;
Pic2Box2d game;
public MenuScreen(Pic2Box2d gameH)
{
this.game = gameH;
stage = new Stage(0, 0, true);
table = new Table();
}
public void create()
{
final TextFieldStyle fieldStyle = new TextFieldStyle(new BitmapFont(), Color.WHITE, new BitmapFont(), Color.GRAY, null, null, null);
textField = new TextField("path", fieldStyle);
final TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
buttonStyle.fontColor = Color.WHITE;
buttonStyle.pressedOffsetY = 1f;
buttonStyle.downFontColor = new Color(0.8f, 0.8f, 0.8f, 1f);
button = new TextButton("Übernehmen", buttonStyle);
button.setClickListener(new ClickListener() {
#Override
public void click(Actor actor, float x, float y) {
if(textField.getText() != "" && textField.getText() != "path")
{
texture = new Texture(textField.getText());
game.setScreen(new Workscreen(texture));
}
}
});
table.row();
table.add(textField);
table.add(button);
stage.addActor(table);
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 1);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
}
#idaNakav is correct about using show() and setting the Stage dimensions. However, that will still give you a black screen because your table doesn't have any size. Try changing your show() code to include table.setFillParent(true).
It should look something like this (code based on the example that you provided).
#Override
public void show() {
final TextFieldStyle fieldStyle = new TextFieldStyle(new BitmapFont(), Color.WHITE, null, null, null);
textField = new TextField("path", fieldStyle);
final TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
buttonStyle.fontColor = Color.WHITE;
buttonStyle.pressedOffsetY = 1f;
buttonStyle.downFontColor = new Color(0.8f, 0.8f, 0.8f, 1f);
button = new TextButton("Übernehmen", buttonStyle);
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
if (textField.getText() != "" && textField.getText() != "path") {
texture = new Texture(textField.getText());
game.setScreen(new Workscreen(texture));
}
}
});
table.row();
table.add(textField);
table.add(button);
// Make the table fill the stage.
table.setFillParent(true);
stage.addActor(table);
}
When screen is becoming the current screen show method is called (not create like in your case), try changing your create method to show
also you are init the stage wrong , you should send it width and height , you sent 0,0
try something like
stage = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),false);