I am trying to disable a TextButton that I've coded as follows:
// Setup new game button
final TextButton newGameButton = new TextButton("New Game", style);
newGameButton.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
Tyr.getInstance().setScreen(new GameScreen());
AudioHelper.stopMusic();
return true;
}
});
table.add(newGameButton).spaceBottom(BUTTON_SPACE);
table.row();
However, the "setDisabled(true)" function doesn't seem to be working. Is there some other way that I can accomplish this task?
Found a solution: "newGameButton.setTouchable(Touchable.disabled);"
Related
I have a play button like this;
private void drawPlayButton() {
playButton = new ImageButton(new TextureRegionDrawable(playTexture), new TextureRegionDrawable(pressTexture));
stage.addActor(playButton);
playButton.setPosition(UIConstants.PLAY_X, UIConstants.PLAY_Y, Align.center);
playButton.addListener(new ActorGestureListener() {
#Override
public void tap(InputEvent event, float x, float y, int count, int button) {
super.tap(event, x, y, count, button);
game.setScreen(new GameScreen(game));
}
});
}
I want to add scale in and out effect to this button using Actions.
I am not that familiar with Actions,I tried something like this;
float duationsec= 0.5f;
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),
Actions.scaleTo(1f, 1f, duationsec)));
img.setOrigin(Align.center);
stage.addActor(playButton);
But this is not giving any effect to the button also same code works for an Image.Is this because button uses drawable texture?
How can I give this effect to a button using Actions?
Also my code is working for one time only.I am calling it in show().If I call it in render(),it works in an abnormal manner.I want this effect to be displaying forever.
Is it possible to achieve this?
Any help would be appreciated.
For performance reason most scene2d.ui groups have transform set to false by default and ImageButton is part of that ui group so you've to enable transform by using setTransform(..) method.
For more detail you can check
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#rotation-and-scale
float duationsec= 0.5f;
playButton.setOrigin(Align.center);
playButton.setTransform(true); // <-- Enable Transform
stage.addActor(playButton);
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec)));
EDIT
If you want to scale up/down on user Click, Do in this way :
playButton.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec), Actions.run(new Runnable() {
#Override
public void run() {
game.setScreen(new GameScreen(game));
}
})));
super.clicked(event, x, y);
}
});
Is it possible to allow the user to touch an actor only one time.
I've tried using boolean but the problem is I write addListener in the class constructor and I want it to be here not in a separate method
I believe you can remove the listener like this, I have not tested it though and no time for it right now.
final Table t = new Table();
t.addListener(ClickListener listener = new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
//Do stuff
//...
//remove this listener
t.removeListener(this);
}
});
I am trying to display a Dialog every time the keys BACK or SCAPE are pressed. However the event is only been caught once, dialog is shown but if I close it by pressing my button NO, then it will never appear again until I go to another screen.
This is how I catch the KeyUp event:
#Override
public boolean keyUp(int keycode) {
if (keycode == Keys.BACK || keycode == Keys.ESCAPE) {
dialog.setVisible(true);
}
return false;
}
This is my button inside of the Dialog:
btnNo.addListener(
new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button){
dialog.setVisible(false);
}
});
If you have any idea ,please let me know...
Check out the following site they have clear description on how to use Dialog in LibGDX http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Dialog.html
My issue has been resolved with the following block code in the Render():
if (Gdx.input.isKeyPressed(Keys.BACK) || Gdx.input.isKeyPressed(Keys.ESCAPE)){
Gdx.input.setCatchBackKey(true);
dialog.setVisible(true);
}
In my game in Libgdx I want to change ImageButton image on click. I think this should be easy, but I have lost hours on this. :)
public void show() {
buttonSound = new ImageButton(skin.getDrawable("sound_off"));
buttonSound.addListener(new onSoundListener());
}
class onSoundListener extends InputListener {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
buttonSound.setBackground(skin.getDrawable("btn_sound"));
}
}
This doesn't work. Can someone help me with this?
Thanks
I found solution for my problem by setting checked image
ImageButton (Drawable imageUp, Drawable imageDown, Drawable imageChecked)
and then
if (gameData.isSound())
buttonSound.setChecked(false);
else
buttonSound.setChecked(true);
I want to use scene2D, in libGDX, to detect click on object.
I have this simple Game sample :
Stage stage;
#Override
public void create()
{
this.stage = new Stage(1280, 720, true);
MyActor actor = new MyActor();
Gdx.input.setInputProcessor(stage);
actor.setTexture(new TextureRegion(new Texture(Gdx.files.internal("plateau.jpg"))));
actor.setScale(0.1f);
stage.addActor(actor);
actor.addListener(new InputListener()
{
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
System.out.println("down"+Math.random());
return true;
}
});
}
#Override
public void render()
{
stage.act();
stage.draw();
}
MyActor is a simple class extending Actor with a rendertexture.
The Event is working fine at start.
But when resizing the frame, while the Actor is streched as wanted, the coordinates of the click are not updated and thus misplaced.
How to make the stage using the new size of the items as base to the event ?
You need to update the viewport of your stage in resize method of your application like this:
public void resize(int width, int height) {
stage.setViewport(stage.getWidth(), stage.getHeight(), false, 0, 0, width, height);
}
What worked for me was similar to what mentioned above but you have to modify it slightly:
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
You shouldn't use the Gdx.graphics.width and height but really use what resize() is providing to you.
You have to use camera.project() method for this purpose I think.
camera.project(Vector3 worldPoint);
Does this works for you?