I am new to libGDX. I am trying to create a custom button by extending com.badlogic.gdx.scenes.scene2d.ui.Button.
I want all the button related logic in this class. But I am not getting how to make the click work. I read many tutorials regarding adding Event Listeners but nothing is working.
public class RestartButton extends Button {
public RestartButton(ButtonStyle style) {
super(style);
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.draw(TextureProvider.getInstance().getRestart(), 175, 100);
}
}
And i am trying to add my button in the screen(i.e in show method) like this
RestartButton restartButton;
restartButton=new RestartButton(new ButtonStyle());
Stage stage;
stage.addActor(restartButton);
I am able to see my button on the screen. Now what i want to do is add some code which gets invoked when button is clicked or touched. Can someone please help ?
restartButton = new RestartButton(new ButtonStyle());
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
System.out.println("Restart clicked!");
}
});
stage.addActor(restartButton);
It does not work because you need to setBounds for your Button. If you wanted to draw the button in the position (175, 100) you could just create a Button directly from Button Class and call
button.setBounds(x, y, width, height);
Then adding the listener will work because now your button will actually have a position and an area in the stage. If you still need to extend the button class for your own reasons, you can set the bounds in the extended class ditectly or you can pass another argument in your RestartButton class. Similar to:
public RestartButton(ButtonStyle style, Vector2 position, Vector2 size) {
super(style);
this.setBounds(position.x, position.y, size.x, size.y);
}
Then the button will automatically be drawn to the position you want without the need of overriding draw method. add the listener by using this.addListener(yourListener);
Hope it helps.
Related
So I have a game I am working on and once you finish playing I want to be able for the user to tap on the "Play Again" button and be able to reset at the start.
To do this I create a Rectangle over the Texture and use the contains() method.
if(cloud.contains((float)Gdx.input.getX(),(float)(Gdx.graphics.getHeight()-Gdx.input.getY()))){
reset();
}
Reset method:
public void reset(){
speed=0;
paraY = Gdx.graphics.getHeight() - para[parachuteState].getHeight();
gameState=0;
backY = 0-Gdx.graphics.getHeight();
bach=0;
Gdx.input.setCursorPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
}
So what is happening is the program recognizes the button being pressed and resets but when the game is over again it automatically resets without displaying the end screen. I think that the cursor is not being moved to the center and is instead remaining on top of the button. Am I incorrectly using the setCursorPosition() method or is there another way to do this?
The button would be just right. It might looks more complex, but it's recommended way to do what you want to do.
So, to create a button you should do something like this:
Skin skin = new Skin();
skin.addRegions(uiTextureAtlas);
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = skin.getDrawable("textureName");
buttonStyle.font = font;
Button button = new Button(buttonStyle);
button.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
reset();
return true;
}
});
button.setSize(100, 100);
button.setPosition(300, 400);
then in your Screen class you create
Stage stage = new Stage();
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
I am making a game where you have two buttons to rotate the player, one to left and the other one to right. I'm using a TextButton in LibGDX. My problem is that the method clicked(InputEvent event, float x, float y) in ClickListener is only called once it's clicked. I want an event to be called repeatedly as long as it's held down. Here is my code:
TextButton btnLeft = new TextButton("<", styleButton);
TextButton btnRight = new TextButton(">", styleButton);
btnLeft.setSize(100, 100);
btnRight.setSize(100, 100);
btnLeft.setPosition(25, 25);
btnRight.setPosition(200, 25);
btnLeft.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
player.rotate(-1);
System.out.println("Left");
}
});
btnRight.addListener(new ClickListener() {
Override
public void clicked(InputEvent event, float x, float y) {
player.rotate(1);
System.out.println("Right");
}
});
stage.addActor(btnLeft);
stage.addActor(btnRight);
The listener is not good place to perform continuous actions since it is by definition asynchronous mechanism. The place to perform some action like this is render() method of Screen or act() method of actor.
Although you can use listener to check a state of actor (is it pressed or not) in this way:
//Global instance
ClickListener listenerLeft;
//show() method
...
listenerLeft= new ClickListener();
btnLeft.addListener(listenerLeft);
...
//render() method
...
if(listenerLeft.isPressed())
//perform turning left
...
The second option is to implement ClickListener's touchUp and touchDown methods to change some flag and then check it in render but it would not be doing anything new.
Worth to notice is than both ClickListener and DragListener have touchDragged method that is something about what you want but works only if the mouse/finger is moving when touching actor
listener = new DragListener(){
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer)
{
System.out.println("Left");
}
};
Keeping touching is not an action - no action = nothing to listen
I am working on a GUI application with Java and SWT.I have createad a main window with a canvas in which I load an image (well it's a PDF page converted to image).I have attached a PaintListener to the canvas and when i drag the mouse onte the canvas I am able to draw a rectangle.
When i release the left button on mouse, I want a dialog window to came out to "fine setting" the rectangle area, so I made a dialog with 4 spinner (x, y, width and height).
Now I want to click on the spinners and the canvas redraw the changed rectangle area.I tried to pass the canvas object to the dialog window and attach a second paint listener to it (So I have one paintlistener from MainWindow.java and another from Dialog.java), but it's no working.
The problem is that I have multiple rectangle drawn on the canvas, and if I call canvas.redraw() from dialog window, the rectangles already drawn on canvas "disappeared".What is the best practise in such situation?
I thinks to put the dialog window "in the toolbar", that is put the 4 spinners in the toolbar (or another area in the main window) so I have only one paint listener and get rid of the dialog, but I prefer the dialog 'cause it is impratical to drag a rectangle onto canvas and then move the mouse to click on toolbar.
Thanks in advance, Mauro
Your main window should implement custom listener for example:
public interface PreferencesListener {
public void updatePreference(PreferencesEvent e)
}
Then create a manager which control updates:
public class PreferencesController {
private static PreferencesController instance = new PreferencesController();
private List<PreferencesListener> preferencesListeners;
private PreferencesController() {
preferencesListeners = new ArrayList<PreferencesListener>();
}
public static PreferencesController getInstance() {
return instance;
}
public void addPreferenceListener(PreferencesListener listener) {
preferencesListeners.add(listener);
}
public void notify(int x, int y, int w, int h) {
PreferencesEvent e = new PreferencesEvent(x, y, w, h);
for(final PreferencesListener listener: preferencesListeners) {
listener.updatePreference(e);
}
}
}
At the moment of creation main window register it in controller:
PreferencesController.getInstance().addPreferenceListener(this);
When you change value in dialog trigger notify:
PreferencesController.getInstance().notify(x,y,w,h);
And then finally implement updatePreference(PreferencesEvent e) from PreferencesListener. From this moment whenever values x y w h are changed your window will be notified. From my point of view it's a good solution because your window and dialog don't even know each other exists, and you could easily add another component that react to preference change.
I'm having troubles with my Actor objects. When I click on the Button Actor, I want it to switch the Screen. When I do this, and I return to the last screen, the button that I clicked is still being hovered until I move my mouse again:
The code that I have for it:
// Load Game
textButtons[1].addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
// GameState is a subclass of Screen,
// GameStateManager helps me to manage my GameStates.
game.setGameState(GameStateManager.get("screenLoadGame"));
}
});
I've tried to "trick" it to fix this, by using the keyUp method, and doing the following:
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
int[] mousePos = { Gdx.input.getX(), Gdx.input.getY() };
Gdx.input.setCursorPosition(0, 0);
stage.act(1);
game.setGameState(GameStateManager.get("screenLoadGame"));
Gdx.input.setCursorPosition(mousePos[0], mousePos[1]);
}
But it doesn't work.
In the end, I want to be able to switch from one Screen back to the Main Menu Screen, and have the button as its default sprite. Is there a way to set an Actor's (Button's) Sprite?
NOTE: For the sprites (in the Json file) The up/down is how "Start New Game" shows it. The over sprite is when it has the gray background (like "Load Game").
Use a ChangeListener instead of a ClickListener. This will also allow your buttons to behave like buttons are expected to (if you click down and move your cursor off the button before releasing, it won't fire).
You can use a single ChangeListener for many buttons quite easily to keep your code tidy.
ChangeListener changeListener = new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
if (actor == startNewGameButton){
startNewGame();
} else if (actor == loadGameButton){
loadGame();
} else if (actor == creditsButton){
showCreditsScreen();
}
}
};
And then to add it to a button:
startNewGameButton.addListener(changeListener);
I am making a LibGdx game. I have a main menu with a button that sends the user to the play screen. When at play screen though, the game reacts as it would when the user clicks in the spot that the button is located in from the previous screen. Here is the code that defines the button and switches screens:
button = new TextButton("Play", button_text);
button.setWidth(Gdx.graphics.getWidth() / 4);
button.setHeight(Gdx.graphics.getHeight() / 8);
button.setPosition((Gdx.graphics.getWidth() / 2) - (button.getWidth() / 2), (Gdx.graphics.getHeight() / 2) - (button.getHeight() / 2));
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
button.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
game.setScreen(new PlayScreen(game));
dispose();
return true;
}
});
Here is the dispose method for this screen:
#Override
public void dispose() {
stage.dispose();
}
So how do I clear all input before moving from one screen to the next?
Add on the dispose method
Gdx.input.setInputProcessor(null);
and will be fixed
Upon further research I have discovered a solution to my problem.
To solve this problem, in the constructor of the new screen I set the input proccessor to a new instance of an implementation of the GestureListener interface.
private MyGestureListener myGestureListener;
public PlayScreen(Game game) {
this.game = game;
myGestureListener = new MyGestureListener();
Gdx.input.setInputProcessor(new GestureDetector(myGestureListener));
}
Giving each screen its own instance of a GestureListener will avoid the problem of input being carried on from one screen to the next.