Hi can anyone tell me how to make a click able buttonIn Libgdx For a mobile device that does something if you touch it
Asked
Active
Viewed 1,643 times
-3
-
may be helpful https://stackoverflow.com/a/21494340/4212632 – Ryan Oct 27 '17 at 03:28
-
Nop can't really say it was – Joseph Austin Oct 27 '17 at 03:35
-
1`For a mobile device that does something if you touch it` ? – Abhishek Aryan Oct 27 '17 at 04:30
-
Will if you touch a texture / Sprite it should something – Joseph Austin Oct 27 '17 at 05:30
-
So you want someone to provide the code? – Mark Fitzgerald Oct 27 '17 at 08:39
-
Iim would for say if you have a play button on screen and it is touched it would do something – Joseph Austin Oct 27 '17 at 16:50
2 Answers
2
This is how you can add a Button without adding any external skin
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = yourCustomFont;
textButtonStyle.fontColor = Color.WHITE;
stage.add(new TextButton("Custom Btn ", textButtonStyle));

Hitesh Sahu
- 41,955
- 17
- 205
- 154
1
This is a minimal example of what you are trying to achieve with comments
package com.mygdx.gtest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Actor;
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.ChangeListener;
public class Test extends ApplicationAdapter{
private Skin skin;
private TextButton button;
private Stage stage;
@Override
public void create() {
//make a stage for your button to go on
stage = new Stage();
//load a skin(a collection of styles for objects)
// skin is from gdx-skins (https://github.com/czyzby/gdx-skins)
skin = new Skin(Gdx.files.internal("neon-ui.json"));
//create your button
button = new TextButton("Button1", skin);
//add it to your stage
stage.addActor(button);
// add a listener to your buttons so it does something when clicked
button.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println("I was clicked");
}
});
// set the sgae as the input processor so it will respond to clicks etc
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
//clear the screen
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// tell stage to do action and then draw itself
stage.draw();
stage.act();
}
}

dfour
- 1,376
- 1
- 12
- 16
-
This looks good but I don't have time to do it at the moment I will let you know soon – Joseph Austin Oct 27 '17 at 16:29
-
-
What doesn't work about it? it works fine on mine printing out "I was clicked" when clicked – dfour Oct 28 '17 at 08:28