LIBGDX making 5 different buttons on one screen - java

I have 5 buttons i need to make do different things but they don't and i need to know how to make them do it.
Here is my code;
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
Button play, option, quit, custom, store, menu;
public MainMenu(CrazyZombies game) {
this.game = game;
}
public void create () {
stage = new Stage();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
batch.begin();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null)
stage = new Stage(width, height, true);
stage.clear();
stage.setViewport(854, 480, true);
stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0);
Gdx.input.setInputProcessor(stage);
/**
* quit Button
*/
TextButtonStyle styleQuit = new TextButtonStyle();
styleQuit.up = skin.getDrawable("8layer");
styleQuit.down = skin.getDrawable("8layer");
quit = new Button(styleQuit);
quit.addListener(new 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) {
}
});
/**
* End quit Button
*/
/**
* store Button
*/
TextButtonStyle styleStore = new TextButtonStyle();
styleStore.up = skin.getDrawable("9layer");
styleStore.down = skin.getDrawable("9layer");
store = new Button(styleStore);
store.addListener(new 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) {
game.setScreen(new StoreScreen(game));
}
});
/**
* End store Button
*/
/**
* customs Button
*/
TextButtonStyle styleCustom = new TextButtonStyle();
styleCustom.up = skin.getDrawable("10layer");
styleCustom.down = skin.getDrawable("10layer");
custom = new Button(styleCustom);
custom.addListener(new 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) {
game.setScreen(new CustomScreen(game));
}
});
/**
* End customs Button
*/
/**
* Options Button
*/
TextButtonStyle styleOptions = new TextButtonStyle();
styleOptions.up = skin.getDrawable("11layer");
styleOptions.down = skin.getDrawable("11layer");
option = new Button(styleOptions);
option.addListener(new 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) {
game.setScreen(new OptionScreen(game));
}
});
/**
* End Options Button
*/
/**
* Play Button
*/
TextButtonStyle stylePlay = new TextButtonStyle();
stylePlay.up = skin.getDrawable("7layer");
stylePlay.down = skin.getDrawable("7layer");
play = new Button(stylePlay);
play.addListener(new 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) {
Gdx.app.log(CrazyZombies.LOG, "un-touched");
game.setScreen(new GameScreen(game));
}
});
/**
* End Play Button
*/
/**
* start Background
*/
TextButtonStyle styleMenu = new TextButtonStyle();
styleMenu.up = skin.getDrawable("background");
menu = new Button(styleMenu);
/**
* End Background
*/
stage.addActor(menu);
stage.addActor(play);
stage.addActor(option);
stage.addActor(store);
stage.addActor(custom);
stage.addActor(quit);
}
#Override
public void show() {
Audio.playMusic(true);
batch = new SpriteBatch();
atlas = new TextureAtlas("data/mainmenu/mainmenu.pack");
skin = new Skin();
skin.addRegions(atlas);
}
#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();
Audio.dispose();
}
public void playButton(Button play) {
}
}
So my 5 buttons are set up and there actions and listeners but now they do not do anything and when i single one button out to test it works but the button can be clicked from any where on the screen so i think it is an issue with button areas but i do not know how to set it up.
I have tryed .getheight(), .getWidth etc. but it is still the same. Although in my texture atlas the height and width for all the images is the same as it all makes one image could this be my issue ?

For your first problem, which is that your buttons doesn't do anything, I think it's because the event is propagated to all of your buttons, probably in the order they have been added to the Stage. The problem is that your touchDown() method returns true. It means that the propagation should stop and then the touchDown() method of your others actors doesn't get called.
You feel like it does nothing but in fact it does something, it's just that your touchDown() method is empty.
For you second problem, it's probably because your didn't set the size of your actors.

Actually it's more simple than that. You need to set the bounds of your buttons. Use the .setBounds(x,y,width,height) method of the button, they do not have a size yet. The click area should be right after setting the bounds.
Take a look at Table layout libgdx and use a table.

Related

Add multiple InputListener to Actor libgdx

I have a problem with InputListener.
I have create class who extend TextButton to make TextButtons with borders and one default Inputlistener. And in my main code i want to add one more Inputlistener to set a new screen when it is pressed (in my code it's just a print to watch if that works) but only the touchDown works...
My TextButtonWithBorder class :
public class TextButtonWithBorder extends TextButton {
ShapeRenderer shapeRenderer = new ShapeRenderer();
public TextButtonWithBorder(String text, Skin skin) {
super(text, skin);
this.setTransform(true);
this.addReduceClickListener();
}
public TextButtonWithBorder(String text, Skin skin, String styleName) {
super(text, skin, styleName);
this.setTransform(true);
this.addReduceClickListener();
}
public TextButtonWithBorder(String text, TextButtonStyle style) {
super(text, style);
this.setTransform(true);
this.addReduceClickListener();
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.end();
shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(getX(),getY(),getWidth()*getScaleX(),getHeight()*getScaleY());
shapeRenderer.end();
batch.begin();
super.draw(batch, parentAlpha);
}
public void setCenter(float x,float y)
{
setPosition(x-getWidth()/2*getScaleX(),y-getHeight()/2*getScaleY());
}
public void setCenter(Vector2 center)
{
setPosition(center.x-getWidth()/2*getScaleX(),center.y-getHeight()/2*getScaleY());
}
public Vector2 getCenter()
{
return new Vector2(getX()+getWidth()/2*getScaleX(),getY()+getHeight()/2*getScaleY());
}
public void addReduceClickListener()
{
addListener((new InputListener(){
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Vector2 center = getCenter();
setScale(0.9F);
setCenter(center);
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
Vector2 center = getCenter();
setScale(1F);
setCenter(center);
}
}));
}
public void dispose()
{
shapeRenderer.dispose();
}
}
And My main code :
public class MainMenuScreen implements Screen {
final PongAndroid game;
Stage stage;
BitmapFont font;
TextButtonWithBorder buttonOnePlayer;
TextButtonWithBorder buttonTwoPlayers;
TextButtonWithBorder buttonAbout;
TextButtonWithBorder buttonExit;
Label title;
ImageButton options;
public MainMenuScreen(final PongAndroid game) {
this.game=game;
stage = new Stage(game.viewport,game.batch);
Gdx.input.setInputProcessor(stage);
// Styles
font = new BitmapFont(Gdx.files.internal("MVboli50.fnt"));
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = font;
ImageButton.ImageButtonStyle imageButtonStyle = new ImageButton.ImageButtonStyle();
// Configure Actors
// title
title = new Label("Pong Android",labelStyle);
title.setFontScale(2f);
title.setPosition(game.WIDTH/2-title.getWidth()/2*title.getFontScaleX(),game.HEIGHT-title.getHeight()-(game.HEIGHT*0.15f));
// buttonOnePlayer
buttonOnePlayer = new TextButtonWithBorder("1 Player",textButtonStyle);
buttonOnePlayer.setWidth(game.WIDTH*0.70f);
buttonOnePlayer.setHeight(buttonOnePlayer.getHeight()*1.2f);
buttonOnePlayer.setPosition(game.WIDTH/2-buttonOnePlayer.getWidth()/2,title.getY()-title.getHeight()/2-buttonOnePlayer.getHeight()-game.HEIGHT*0.05f);
//buttonTwoPlayer
buttonTwoPlayers = new TextButtonWithBorder("2 Players",textButtonStyle);
buttonOnePlayer.setTransform(true);
buttonTwoPlayers.setWidth(buttonOnePlayer.getWidth());
buttonTwoPlayers.setHeight(buttonTwoPlayers.getHeight()*1.2f);
buttonTwoPlayers.setPosition(buttonOnePlayer.getX(),buttonOnePlayer.getY()-buttonOnePlayer.getHeight()-game.HEIGHT*0.05f);
//buttonAbout
buttonAbout = new TextButtonWithBorder("About",textButtonStyle);
buttonOnePlayer.setTransform(true);
buttonAbout.setWidth(buttonTwoPlayers.getWidth()/2-game.WIDTH*0.05f);
buttonAbout.setHeight(buttonAbout.getHeight()*1.2f);
buttonAbout.setPosition(buttonTwoPlayers.getX(),buttonTwoPlayers.getY()-buttonAbout.getHeight()-game.HEIGHT*0.05f);
//buttonExit
buttonExit = new TextButtonWithBorder("Exit",textButtonStyle);
buttonOnePlayer.setTransform(true);
buttonExit.setWidth(buttonAbout.getWidth());
buttonExit.setHeight(buttonExit.getHeight()*1.2f);
buttonExit.setPosition(buttonAbout.getX()+buttonAbout.getWidth()+game.WIDTH*0.1f, buttonAbout.getY());
// Add listeners to Actors
buttonExit.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return super.touchDown(event, x, y, pointer, button);
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
super.touchUp(event, x, y, pointer, button);
System.out.println("up");
}
});
// Add Actors to stage
stage.addActor(title);
stage.addActor(buttonOnePlayer);
stage.addActor(buttonTwoPlayers);
stage.addActor(buttonAbout);
stage.addActor(buttonExit);
//stage.addActor();
}
#Override
public void show() {
}
#Override
public void render(float delta) {
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
buttonOnePlayer.dispose();
buttonTwoPlayers.dispose();
buttonAbout.dispose();
buttonExit.dispose();
}
}
My question is how to add multiple InputListener to an Actor ?
Return true for both InputListeners to handle the touchUp event. This is my output when clicking once:
listener A: touchDown
listener B: touchDown
listener A: touchUp
listener B: touchUp
The code:
private SpriteBatch batch;
private Viewport viewport;
private Stage stage;
private Texture texture;
private BitmapFont font;
#Override
public void create() {
batch = new SpriteBatch();
viewport = new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
stage = new Stage( viewport, batch );
texture = new Texture( "badlogic.jpg" );
font = new BitmapFont();
Gdx.input.setInputProcessor( stage );
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(
new TextureRegionDrawable( new TextureRegion( texture ) ), null, null, font );
TextButton textButton = new TextButton( "text", textButtonStyle );
textButton.setPosition(
0.5f * Gdx.graphics.getWidth() - 0.5f * textButton.getWidth(),
0.5f * Gdx.graphics.getHeight() - 0.5f * textButton.getHeight()
);
textButton.addListener( new InputListener(){
#Override
public boolean touchDown( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener A", "touchDown" );
return true;
}
#Override
public void touchUp( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener A", "touchUp" );
}
} );
textButton.addListener( new InputListener(){
#Override
public boolean touchDown( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener B", "touchDown" );
return true;
}
#Override
public void touchUp( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener B", "touchUp" );
}
} );
stage.addActor( textButton );
}
#Override
public void dispose() {
batch.dispose();
stage.dispose();
texture.dispose();
font.dispose();
}
#Override
public void resize( int width, int height ) {
viewport.update( width, height, true );
}
#Override
public void render() {
Gdx.gl.glClearColor( 0, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
stage.act( Gdx.graphics.getDeltaTime() );
stage.draw();
}
Buttons in LibGDX already have built-in InputListeners that robustly handle different button and hover states. Instead of adding your own InputListener, you should add a ChangeListener to respond to button presses.

Use actions instead Drawable with buttons in libGDX?

I'm developing a game in libGDX and I want to add custom buttons. When the button is pressed, I want to add an action like scaleBy, because it looks better like that in Android. I made a class called Trigger which is an empty actor with a listener that add actions to the actor when the Trigger is pressed, entered, released, exited... So I create a Group with two actors: a TextButton (Touchable.disabled) and the Trigger.
It actually works but I have a problem: I can't add groups in Dialog's button method.
I think a better solution is creating a class which extends TextButton or Button, but I don't know how to do it.
If you want the code, just tell.
Thanks.
EDIT: I tried to make the class:
private Runnable runnable;
private UIScreen screen;
public static TextButtonStyle transformStyle(Skin skin) {
TextButtonStyle s = skin.get(TextButtonStyle.class);
TextButtonStyle style = new TextButtonStyle(s.up, null, null, s.font);
return style;
}
public static TextButtonStyle transformStyle(TextButtonStyle style) {
TextButtonStyle s = new TextButtonStyle(style.up, null, null, style.font);
return s;
}
public DTextButton(String text, Skin skin, UIScreen screen, Runnable runnable) {
super(text, transformStyle(skin));
this.runnable = runnable;
this.screen = screen;
addListener(new ButtonListener());
setOrigin(Align.center);
setTransform(true);
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}
public class ButtonListener extends ClickListener {
private boolean isPressed = false;
private int lastButton = 0;
public void press() {
screen.setButtonPressed(true);
UIFactory.applyPressedAction(DTextButton.this);
isPressed = true;
}
public void release(){
UIFactory.applyReleasedAction(DTextButton.this);
isPressed = false;
}
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if(button == Buttons.LEFT)
press();
return true;
}
#Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
lastButton = (Gdx.input.isButtonPressed(Buttons.LEFT)) ? Buttons.LEFT : -1;
if(pointer == 0 && !isPressed && screen.wasButtonPressed())
press();
}
#Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
if(toActor == DTextButton.this && lastButton == Buttons.LEFT){
Gdx.app.postRunnable(runnable);
}
if(pointer == 0 && isPressed)
release();
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
lastButton = button;
if(pointer == 0 && isPressed && button == Buttons.LEFT)
release();
screen.setButtonPressed(false);
}
}
UIScreen:
public interface UIScreen {
/**
* Return if any UI is pressed.
* #return return buttonPressed
*/
public boolean wasButtonPressed();
/**
* Set if any UI is pressed.
* #param pressed if pressed or not.
*/
public void setButtonPressed(boolean pressed);
}
UIFactory:
public static void applyPressedAction(Actor actor) {
actor.addAction(Actions.scaleBy(-0.2f, -0.2f, 0.2f, Interpolation.pow2Out));
}
public static void applyReleasedAction(Actor actor) {
actor.addAction(Actions.scaleBy(0.2f, 0.2f, 0.3f, Interpolation.swingOut));
}
So I only used style.up and I added the same Trigger's listener. The listener doesn't work well. Any suggestion?
I think you're over-complicating this. You can merely add an input listener to the button and make sure it's set to transform. And you probably want its origin centered. No need for subclasses or wrapper Groups or anything like that.
Also, you want to scale to, not by, to avoid error building up as you change states multiple times.
Here's a helper method:
public static void makeButtonScale (final Button button, final float hoverScale, final float pressedScale, final float duration){
button.setTransform(true);
button.addListener(new ClickListener(){
void scaleTo (float targetScale){
button.setOrigin(Align.center);
button.clearActions();
button.addAction(Actions.scaleTo(targetScale, targetScale, duration, Interpolation.fade));
}
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
super.touchDown(event, x, y, pointer, button);
scaleTo(pressedScale);
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
scaleTo(isOver() ? hoverScale : 1f);
}
public void enter (InputEvent event, float x, float y, int pointer, Actor fromActor) {
super.enter(event, x, y, pointer, fromActor);
scaleTo(isPressed() ? pressedScale : hoverScale);
}
public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
scaleTo(1f);
}
});
Example usage:
makeButtonScale(myDialogButton, 1.2f, 1.5f, 0.2f);
Note, this will not look very good unless you are using a linear mag filter on your texture atlas.

Libgdx MainMenu is not responding

I defined a mainmenu class where I am Trying to show some images inside a table and make them respond for touches but when I define the call the class (just call it) inside the constructor of the main screen, the controls don't respond although I defined the mainmenu class in the same way of defining the controls class.
(notice when I use mainmenu object inside the main screen it renders the images and the controls but they don't respond for any click).
the main problem is happened when I call the mainmenu, the game is running but controls don't respond for touch. here is the code of the two classes:
public class Controller {
public Viewport viewport;
public Stage stage;
public boolean upPressed;
public boolean leftPressed;
public boolean rightPressed;
public boolean pausePressed;
public static boolean visiblity;
public Image buttonUp;
public Image buttonDown;
public Image buttonLeft;
public Image buttonRight;
public Image buttonpause;
public OrthographicCamera camera;
public Table table;
public Table table1;
public Table table2;
//Constructor.
public Controller(SpriteBatch spriteBatch) {
camera = new OrthographicCamera();
viewport = new FitViewport(Fruits.V_WIDTH, Fruits.V_HIEGT, camera);
stage = new Stage(viewport, spriteBatch);
Gdx.input.setInputProcessor(stage);
visiblity=false;
//Buttons with images.
buttonUp = new Image(new Texture("Pause.png"));
buttonUp.setSize(30, 30);
buttonUp.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
upPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
upPressed = false;
}
});
buttonLeft = new Image(new Texture("buttonleft.png"));
buttonLeft.setSize(65, 65);
buttonLeft.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = false;
}
});
buttonRight = new Image(new Texture("buttonright.png"));
buttonRight.setSize(65, 65);
buttonRight.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = false;
}
});
buttonpause = new Image(new Texture("pausing.png"));
buttonpause.setSize(30, 30);
buttonpause.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = false;
}
});
//Table with buttons.
table = new Table();
table.left().bottom(); //Align to the left bottom.
table.add(buttonLeft).size(buttonLeft.getWidth(), buttonLeft.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
// table.add(buttonUp).size(buttonUp.getWidth(), buttonUp.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
table.add(buttonRight).size(buttonRight.getWidth(), buttonRight.getHeight());
stage.addActor(table);
table1=new Table();
table1.right().top();
table1.setFillParent(true);
//table1.add();
table1.row().pad(25, 3, 0, 10);
table1.add(buttonUp).size(buttonUp.getWidth(), buttonUp.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
stage.addActor(table1);
table2=new Table();
table2.center();
table2.setFillParent(true);
//table1.add();
table2.row().pad(25, 3, 0, 10);
table2.add(buttonpause).size(buttonpause.getWidth(), buttonpause.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
stage.addActor(table2);
}
public void draw() {
stage.draw();
}
public void resize(int width, int height) {
viewport.update(width, height);
}
public boolean isUpPressed() {
return upPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public boolean isPausePressed() {
return pausePressed;
}
}
code of mainmenu
public class MainMenu{
/* public Viewport viewport;
public Stage stage;
public boolean pausePressed;
public boolean resumePressed;
public boolean exitPressed;
public Image buttonpause;
public Image buttonDown;
public Image buttonresume;
public Image buttonexit;
public OrthographicCamera camera;
public Table table;
public Table table2;
//Constructor.
public MainMenu(SpriteBatch spriteBatch) {
camera = new OrthographicCamera();
viewport = new FitViewport(Fruits.V_WIDTH, Fruits.V_HIEGT, camera);
stage = new Stage(viewport, spriteBatch);
Gdx.input.setInputProcessor(stage);
//Buttons with images.
buttonpause = new Image(new Texture("pausing.png"));
buttonpause.setSize(30, 30);
buttonpause.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = false;
}
});
buttonresume = new Image(new Texture("resume.png"));
buttonresume.setSize(65, 65);
buttonresume.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
resumePressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
resumePressed = false;
}
});
buttonexit = new Image(new Texture("exit.png"));
buttonexit.setSize(65, 65);
buttonexit.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
exitPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
exitPressed = false;
}
});
//Table with buttons.
table2=new Table();
table2.center();
table2.setFillParent(true);
//table2.add();
table2.row().pad(25, 3, 0, 10);
table2.add(buttonpause).size(buttonpause.getWidth(), buttonpause.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
table2.row().pad(25, 3, 0, 10);
table2.add(buttonresume).size(buttonresume.getWidth(), buttonresume.getHeight());
table2.row().pad(25, 3, 0, 10);
table2.add(buttonexit).size(buttonexit.getWidth(), buttonexit.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
stage.addActor(table2);
}
public void draw() {
stage.draw();
}
public void resize(int width, int height) {
viewport.update(width, height);
}
public boolean isPausePressed() {
return pausePressed;
}
public boolean isResumePressed() {
return resumePressed;
}
public boolean isExitPressed() {
return exitPressed;
}
*/
}
There can only be one input processor set at a time, so only your most recent call to Gdx.input.setInputProcessor has any effect. So move that call to the show() method of your screens (if you're implementing Screen, otherwise do whatever the equivalent is in your structure).
You can also use an InputMultiplexer to combine multiple input processors into one if you want them to be able to work at the same time.

InputListener doesn't work with OrthographicCamera and Actors

I have a Stage with OrthographicCamera and also I have a Actor with InputListener setter by addListener (from actor class). The problem is that the actor doesn't proccess input, but if in my screen I delete OrthographicCamera the Actor proccess the input, so, with OrthographicCamera Actor doesn't proccess input but it works if I remove OrthographicCamera.
Any advice?
I have the following code
public class Test implements Screen {
private Game game;
private Stage stage;
private MemoryActor actor;
private AssetManager manager;
private boolean loaded = false;
float width, height;
private OrthographicCamera camera;
public Test(Game game){
this.game = game;
stage = new Stage();
manager = new AssetManager();
manager.load("img.png",Texture.class);
manager.load("img1.png",Texture.class);
InputMultiplexer im = new InputMultiplexer();
im.addProcessor(stage);
Gdx.input.setInputProcessor(im);
height = Gdx.graphics.getHeight();
width = Gdx.graphics.getWidth();
camera = new OrthographicCamera(width, height);
camera.position.set(((width / 2)), ((height / 2)), 0);
camera.update();
stage.setViewport(new ExtendViewport(300,300, camera));
}
public void createActor(){
Texture back = manager.get("img.png", Texture.class);
actor = new MemoryActor(manager.get("img1.png", Texture.class), back,0,0,50,50);
actor.setInputListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
}
});
stage.addActor(actor);
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (manager.update()){
if (!loaded){
createActor();
loaded = true;
}
}
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
and MemoryActor:
public class MemoryActor extends Actor {
...
public MemoryActor(){}
public MemoryActor(Texture texture, Texture texBack, float x, float y, float width, float height){
...
}
public void setInputListener(InputListener il){
addListener(il);
}
#Override
public void draw(Batch batch, float alpha){
...
}
}
Just registering the stage as an InputProcessor isn't enough. You also need to trigger the event processing of Stage via stage.act() in every frame.
Furthermore you need to properly update the stage's Viewport when a resize event occurs. This can be done via stage.getViewport().update(width, height, true). Otherwise the stage will process the events based on incorrect assumptions about the screen size and might also render your stage not the way you want it. The true is important because it will also center the camera on the new screen size, which is necessary in case of UIs.

How to make buttons play sound in Libgdx?

How can I make a TextButton play a sound when the mouse hovers over it? Something like:
TextButton play = new TextButton("Play", textButtonStyle);
play.addListener(new ButtonHoverListener() {
#Override
public void doSomething() {
...
}
});
play.addListener(new InputListener(){
boolean playing = false;
#Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
super.enter(event, x, y, pointer, fromActor);
if (!playing) {
Sound sound = Gdx.audio.newSound(Gdx.files.internal("data/mysound.mp3"));
sound.play(1F);
playing = true;
}
}
#Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
playing = false;
}
});

Categories

Resources