My LibGDX slider won't slide - java

As goes without saying, I'm a complete novice and I'm not too sure what to do.
When I run my application, the slider simply won't slide. I've looked everywhere, and the closest I got was thinking it may be something to do with a knob but I have no idea what specifically. It may be worth noting that I'm using the default LibGDX UI files.
package com.angus;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class Audio extends ApplicationAdapter {
Slider slider;
Skin skin;
Stage stage;
#Override
public void create () {
skin = new Skin(Gdx.files.internal("uiskin.json"));
stage = new Stage(new ScreenViewport());
slider = new Slider(0.00f, 10.00f, 1.00f, false, skin);
stage.addActor(slider);
System.out.println(slider.getStyle());
}
#Override
public void render () {
stage.draw();
}
#Override
public void dispose () {
}
}
Any help would be greatly appreciated! Thank you in advance.

I think this should help:
Gdx.input.setInputProcessor(stage);
This tells LibGDX to handle inputs from stage.

Extending icarumbas' answer, at the beginning of the render() method, you need to clear the screen.
Gdx.gl.glClearColor(red, blue, green, alpha);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
red, blue, green, alpha are all floats of 0-1. If you wanted to make the background color white, you would use: Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
So render clears the screen then draws the slider.

Related

LIBGDX - Table's textButtons are not filling the respective rows

I was recently working on a game which utilises Scene2D until I ran into this problem as shown in this picture:
The bounds in which I'm able to click the button exceed the actual TextButton's area
I've read the documentation for TableLayouts in the GitHub page here and have read different articles about Scene2D in general, and I tried fiddling around with the different functions (eg padding, colSpan) to try to come to a solution but I can't seem to find a work around for this.
Here is the code responsible for the setup and drawing of the table:
table = new Table();
table.setDebug(true);
table.add(graphicsButton).width(200f).height(200f);
table.row();
table.add(audioButton).width(200f).height(200f);
table.row();
table.add(backToTitleButton).width(200f).height(200f);
table.setBounds(0, 0, stage.getWidth(), stage.getHeight());
stage.addActor(table);
And here is the rest of the code (note that I haven't centred the text within the buttons yet, I've only added filler values, also - I've removed the empty functions from the Screen class for convenience)
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.thesameritan.game.GUI.UI;
public class OptionsScreen extends ScreenProperties implements Screen {
private Stage stage;
private Table table;
OptionsScreen (final Game game) {
super();
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
TextButton backToTitleButton = UI.createNewButton("BACK", buttonStyle, 0.8f, 25f, 0.9f); //creates button from another class with text, style, padBottom, padLeft and fontScale
TextButton graphicsButton = UI.createNewButton("GRAPHICS", buttonStyle, 0.9f, 20f, 0.9f);
TextButton audioButton = UI.createNewButton("AUDIO", buttonStyle, 0.9f, 20f, 0.9f);
backToTitleButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
//branch to title
}
});
graphicsButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
//branch to graphics
}
});
audioButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
//branch to options
}
});
table = new Table();
table.setDebug(true);
table.add(graphicsButton).width(200f).height(200f);
table.row();
table.add(audioButton).width(200f).height(200f);
table.row();
table.add(backToTitleButton).width(200f).height(200f);
table.setBounds(0, 0, stage.getWidth(), stage.getHeight());
stage.addActor(table);
}
#Override
public void render (float delta) {
clearScreen(); //from ScreenProperties
stage.draw();
}
#Override
public void dispose() {
disposeObjects(); //from ScreenProperties
stage.dispose();
}
}
What I would like is either minimising the gaps between the buttons without affecting the button's size - which again, I've tried by changing the .width() and .height() values or actually reduce the size of the interactive area (shown in the red lines)
Thank you so much!
The interesting and missing part here is UI.createNewButton.
Instead of setting debug to the outer table, you can debug the button itself: backToTitleButton.setDebug(true). You'll see that the click bounds doesn't exceed the buttons area, but the graphical representation is not correct. I suspect you aren't using a Ninepatch for the Rectangle.

libGDX font being drawn out of the viewport

I'm having problems to draw some text in the screen, and I found out that depending on the viewport size, the text may get out of the display despite being calculating the coordinates as the center of the screen. Here's the code, it is a just slightly modified project as created by libgdxgenerator by default:
package net.iberdroid.libgdxtestfonts;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class LibGdxTestFonts extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
BitmapFont defaultFont;
private OrthographicCamera camera;
private Viewport viewport;
private float textY;
private float textX;
#Override
public void create() {
camera = new OrthographicCamera();
viewport = new FitViewport(
640,
480,
camera);
camera.setToOrtho(false);
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
defaultFont = new BitmapFont();
textX = viewport.getWorldWidth() / 2;
textY = viewport.getWorldHeight() / 2;
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.setProjectionMatrix(camera.combined);
defaultFont.setColor(Color.WHITE);
Gdx.app.log("render", String.format("TextX: %f TextY: %f", textX, textY));
defaultFont.draw(batch, "HELLO WORLD!", textX, textY);
batch.end();
}
#Override
public void dispose() {
batch.dispose();
img.dispose();
}
}
As far as I understand, that should draw a HELLO WORLD! text starting around the center of the screen. And it actually does it with that viewport size. Now, if you try with a bigger viewport, let's say 800x600, the text will move to the right and top, if try even with higher values, it will become a point in which the text will get out the boundaries of the screen by the top-right corner.
The same happens in the opposite direction. The smaller viewport you try, the further from the center and closer to the bottom-left corner the text will appear, until it eventually get out the boundaries too.
So either I am failing to grasp something here, or the BitMap.draw method seems to be ignoring the viewport size and using others that I cannot figure out.
If someone else does it, please, make me know.
Thanks a lot in advance!
P.S. I've tried also with a Hiero generated font and had the same issue.
Adding this seems to solve the problem with the position, though I still have a problem with the scale, but that's a different issue.
#Override public void resize (int width, int height) {
viewport.update(width, height, true);
}

Why isn't my clicklistener working?

Here is my code
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
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.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class MainMenuScreen extends SpaceInvaderScreen {
SpriteBatch batch;
TextureRegion background;
Stage stage;
TextButton playbutton;
float time = 0;
public MainMenuScreen(Game game){
super(game);
}
#Override
public void show(){
batch = new SpriteBatch();
batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
background = new TextureRegion(new Texture("data/cool.jpg"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage = new Stage(new FitViewport(1280, 1080));
Skin uiskin = new Skin(Gdx.files.internal("data/skin/uiskin.json"));
playbutton = new TextButton("Play", uiskin);
playbutton.setWidth(200);
playbutton.setHeight(100);
playbutton.setPosition(980, 620);
playbutton.setBounds(playbutton.getX(), playbutton.getY(), playbutton.getWidth(), playbutton.getHeight());
playbutton.addListener( new ClickListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
game.setScreen( new GameScreen(game));
return true;
};
});
stage.addActor(playbutton);
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.15f, 0.15f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(background, 0, 0);
batch.end();
stage.act();
stage.draw();
time += delta;
}
#Override
public void hide(){
batch.dispose();
background.getTexture().dispose();
}
}
i'm not sure whats going on with the clicklistener as to why it doesn't work. I also tried using the touchDown method but it doesn't work either. Sorry if this sounds like a stupid question, im fairly new to the library
Just two things to try: I can see that you're building the button in the show method. Why? It makes much more sense to have a setup method and build the whole layout of the application / game / whatever in that setup method before showing anything. But this is just a shot in the dark, I don't know this framework you are using and I might be very well wrong.
Also another thing that might cause problems when you keep multiple instances of the same game! Watch out to build the game once and only once, and pass that single instance around. Why is that important? Because there might be a case that you are calling game.setScreen( new GameScreen(game)); but on the wrong instance!
Please post your SpaceInvaderScreen as well so it might be easier to debug it.
And anyway, have you tried debugging the touchDown method? Is it called at all?
Also two more things: ClickListener is a class. Not an interface. So when you are overriding the touchDown method, then you are basically bypassing the parent code in the superclass.
Try calling as the first line in the touchdown method:
super.touchDown(event, x, y, pointer, button);
Also the ClickListener documentation says:
when true is returned, the event is handled (...) This does not affect
event propagation inside scene2d, but causes the Stage event methods
to return true, which will eat the event so it is not passed on to the
application under the stage.
So you should try returning false;

Libgdx tweening issue

I am creating a game and trying to set up a splash screen.
Whenever I render the sprite that i want to tween to by using the sprite.draw method which looks like this:
#Override
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
tm.update(delta);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
Assets.splash_spr_bg.draw(sb);
sb.end();
}
The tweening works great except i can only see 1/4 of the picture on my screen, it is completely out of position.
And whenever I try to use this code in order to render the sprite by using the spritebatch to draw it, which looks like this:
#Override
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
tm.update(delta);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(Assets.splash_spr_bg, 0, 0);
sb.end();
}
I can see the background great, great quality, correct size and position and all that. However, the tweening doesn't work at all; nothing happens.
Why does this not work? How can I fix it?
Here is some other code.
Initializion of the TweenHandler class:
package com.heavenapps.jumpdodge.handlers;
import com.badlogic.gdx.graphics.g2d.Sprite;
import aurelienribon.tweenengine.TweenAccessor;
public class TweenHandler implements TweenAccessor<Sprite>
{
public static final int ALPHA = 1;
#Override
public int getValues(Sprite target, int tweenType, float[] returnValues)
{
switch(tweenType)
{
case ALPHA:
returnValues[0] = target.getColor().a;
return 1;
default:
return 0;
}
}
#Override
public void setValues(Sprite target, int tweenType, float[] newValues)
{
switch(tweenType)
{
case ALPHA:
target.setColor(1, 1, 1, newValues[0]);
break;
}
}
}
Initializion of the sprite/texture:
package com.heavenapps.jumpdodge.handlers;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Assets
{
public static Texture splash_tex_bg;
public static Sprite splash_spr_bg;
public static void init()
{
// Splash Screen
splash_tex_bg = new Texture(Gdx.files.internal("Splash Screen/Background.png"));
splash_tex_bg.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splash_spr_bg = new Sprite(splash_tex_bg);
splash_spr_bg.setOrigin(splash_spr_bg.getWidth() / 2, splash_spr_bg.getHeight() / 2);
splash_spr_bg.setPosition(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
splash_spr_bg.setColor(1, 1, 1, 0);
}
}
Usage of the tweening:
public void fadeSplashScreen()
{
Tween.to(Assets.splash_spr_bg, TweenHandler.ALPHA, 2F).target(1).ease(TweenEquations.easeInBounce).start(tm);
}
You need to set the sprite's position if you want to draw it using your first method. (sprite.draw(spriteBatch)). And you need to use your first method if you want to use the sprite's color, which is being controlled by the tween.
It looks like you did give the background sprite an initial position, but you put it off screen. So change
splash_spr_bg.setPosition(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
to
splash_spr_bg.setPosition(0, 0);
The reason the second method you showed (spriteBatch.draw(sprite, x, y)) draws it in the correct position is that this method of drawing ignores the fact that it's a sprite and just draws the texture region owned by the sprite in whatever position you give it. And this method doesn't fade the sprite because it's ignoring the fact that it is a sprite (which has a color).

Drawing text in LibGDX

I am having a hard time drawing text in LibGDX. Every time I run it, i get a black window. I want the text to be white, but I dont see anything. I have tried finding numerous tutorials and vieos, and still cannot get it to work. I do have a vaild fnt and png file in my assets folder / fonts.
package org.alexwebber.frc.stalk;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Main extends ApplicationAdapter
{
public class HelloWorld implements ApplicationListener
{
private SpriteBatch batch;
private BitmapFont font;
#Override
public void create()
{
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/main.fnt"),
Gdx.files.internal("fonts/main.png"), false);
}
#Override
public void dispose()
{
batch.dispose();
font.dispose();
}
#Override
public void render()
{
batch.begin();
font.setColor(255.0f, 255.0f, 255.0f, 255.0f);
font.draw(batch, "Hello World", 25, 160);
batch.end();
}
#Override
public void resize(int width, int height)
{
}
#Override
public void pause()
{
}
#Override
public void resume()
{
}
}
}
Something that might be impacting you is the removal of the screen clearing:
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.setColor(Color.WHITE);
font.draw(batch, "Hello world", 25, 160);
batch.end();
}
You can also try passing Color.WHITE as above. If the Android device you're using has an extreme screen resolution you may not be seeing the line of text.
Testing via a Desktop application may be a good idea as well, as you can tweak the screen resolution so the text is a larger size.

Categories

Resources