How do you make play again button in libgdx? - java

I have a button on my game where it set the screen on my game screen and it's located on my menu screen. So what happens is that when my character dies it just goes back to the menu screen, what I wan't to do is when I lose there will be just a button that will let you play again and a box that shows your score and the best score, I want to know what that box is called, is it just a texture that will show up when a condition is true? and also how do you make those effect where the button or text will go upwards and stop when they are the correct position (or other effects when they show up on the screen).

I find it unlikely that when your character dies the game automatically goes to the main menu screen again. It's more likely you are doing some tutorial or took some code that tells the program to go back to the main menu when the character dies.
Anyway, in my latest game I simply had a stack actor as the first actor on my stage. There I would just put in the interface and all like someone normally would. When I need a screen on top I would just add it to the base stack. This could be a pause display with a unpause, restart and exit button. Or this could be a game over screen where scores are calculated with exit and replay buttons.
Stage stage;
Stack stack; //scene2D.ui.Stack
Table mainTable;
Table overlayTable;
public GameScreen()
{
stage = new Stage();
stack = new Stack();
mainTable = new Table();
overlayTable = new overlayTable();
stage.addActor(stack);
stack.addActor(mainTable);
stack.addActor(overlayTable);
}
Now just setup the mainTable like you would normally layout your interface/game. The overlayTable is used to display stuff overlayed on your main table. I clear it when it's not needed anymore and build it up again when the player pauses or finishes the level. You can also use separate tables for this like pauzeTable, successTable, failedTable, etc. and hide or display these on demand.
For the effects you simply use Scene2D Actions, MoveToAction or MoveByAction specifically. You can set a action for each button/actor so you have more control over them individually or just a single MoveToAction for the whole table.
Table actionTable = new Table();
//position the table outside the screen
actionTable.setPosition(stage.getWidth(), 0); //Position on the right of the stage
MoveToAction moveAction = new MoveToAction();
moveAction.setPosition(0, 0); //Move from right side of stage inside the stage
moveAction.setDuration(.5f); //Duration of this action
moveAction.setInterpolation(Interpolation.fade); //Fade the movement in and out, many interpolations are supplied by the framework.
actionTable.addAction(moveAction); //Execute Action.

Related

Java Swing - How can I make a frame "mandatory to interact with"?

I'm making a game in Java Swing. Every time the player levels up I display a new frame on top of the one containing the gameplay which has some options on what rewards to get for having leveled up. Due to the nature of my rewards being able to skip choosing some of the upgrades could break the game, so I want to ensure that the user can only resume playing by clicking on one of the 1-3 buttons displayed in the level-up frame.
I have the frame set to undecorated and to appear always on top, but nothing's stopping the user from clicking outside of it to the main gameplay panel and simply pressing esc to resume playing. Instead I want it so the user can only click on the level-up frame out of the 2.
Is there an easy setting I can toggle for this, or do I have to remove remove my keyboard input listener from the first panel to achieve this?

Frame within frame

I'm making a simple arithmetic game.
The first frame is a login screen, next this will close and then open a new frame after the user is logged in.
In the next frame, you can choose buttons which represent the game's modes and when I click a button a new frame opens.
What I am trying to accomplish is that I want is to continue working within this new frame. I don't want to keep moving between frames.
I am trying to make a back button for each mode so I can return to the original modes frame. I am only using the two frames for my program.
Is there a way I can choose the mode then go to a new screen and still be able to go back to the mode selection frame?
You could have 2 JPanels and when you press a button, an action listener does
Frame.remove(Panel1);
Frame.add(Panel2);

Image Click event in Java LibGdx

I am working on a Street Fighter Game. I need to have a character selection screen for the user. How can I do this character selection in Java libGDX? This is my image code. Can I do this using ImageClick event?
splashTexture1 = new Texture(Gdx.files.internal("assets/gui/Character1.PNG"));
splashTexture1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite1 = new Sprite(splashTexture1);
splashSprite1.setX(100);
splashSprite1.setY(180);
My suggestiong is using scene2d for menu or HUD stuff.
Check this out : https://github.com/libgdx/libgdx/wiki/Scene2d
scene2d is a 2D scene graph for building applications and UIs using a hierarchy of actors:
buttons
labels
sliders
text buttons
scrollable views (lists)
tables
your custom widgets
group of actors
You can add ClickListener to an actor. (In your case the actor is a Button)
You can do it in two ways. I'd recommend using both.
Common stuff
You can list images of all characters in a screen. And let the user select any one using one of the following.
Click event
Each image will have a separate ClickListener (If you have problem regarding that, you should search 'How to add clicklisteners to libgdx actors).
On clicking any image, the variable storing selected character (for the rest of the game) will be set with the image that has been clicked. And the screen is changed.
Keyboard event
One of the image will have focus drawn around it. The user can move focus around to other character images using arrow keys. When user presses enter, the character that currently has focus gets selected and the screen is changed.
Hope this helps.
I would recomend to use the Table-Layout from libgdx for it. Add an ImageButton to a Table and add an ClickListener to the ImageButton. Add the same Texture as up and down Texture and you are done. Maybe change it later so the user see that he has clicked a figure.
The Table gives you the ability to arrange those Buttons in the way you like and it's structured. Moreover it is easy to create a dynamic view, that does have new ImageButton for every new Texture you add to a folder for example. Just create a ScrollPane with a the Table of "figures" (which are the ImageButton with an listener) and you can add a bunch of it and let the user scroll down and up to check which he likes.

Open java Applet or Frame inside a parent Applet method and wait for input

I have a Java applet (lets call it parentApplet) with a public method which must return information regarding the status of the performed actions (let's call it getUserInput()) . This method opens another Applet which needs user button input, by adding it as a child with add(childApplet), and afterwards adding itself (the parent) as an ActionListner of the buttons in the childApplet, being able to run other methods when the user clicks on the buttons in the childApplet.
My question is, how can I halt getUserInput() execution until the user has clicked the childApplet buttons?
I tried to have a static variable that tracks the return information, and spinning on a while(var == null) Thread.Sleep(1000); but it blocks the main thread, as it should.
PS: Having the childApplet as an applet can be changed to anything that could better fulfil the requirement of opening another panel on top of the parent applet.
Details on getUserInput()
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and OK/Clear/Cancel buttons. When the user presses OK, I need to get the BufferedImage drawn. Do you know if this can be accomplished by extending a JDialog?
You really need to restructure your app. You can't do it the way you want.
Try creating a JDialog set it up as you like with input fields and an OK/Cancel button.
Then to show the dialog do:
MyDialog dialog = new MyDialog(null, true); //true = modal
//dialog.setModalityType(ModalityType.DOCUMENT_MODAL); //or specify modal here
dialog.setVisible(true); //waits until dialog is closed
if (dialog.wasAccepted()) {
//grab values from dialog
dialog.getCanvas();
}
In the dialog you would have:
private boolean accepted = false;
public boolean wasAccepted() {return accepted;}
public Canvas getCanvas() {return canvas;}
public ? getWhateverElseYouWant() {return ...;}
The OK button would:
accepted=true;
dispose();
The Cancel button would:
accepted=false;
dispose();
The JDialog will pump events while it's visible. So the setVisible() function will halt execution until the dialog is closed.
That should work better, and then you can return many user input fields.
You can even change the JDialog constructor to pass default value(s) in.
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and ok/clear/cancel buttons. When the user presses OK, I need to get the BufferedImage drawn.
First of all, it should not be an applet but a JPanel (it is not impossible to do it as an applet, but also not trivial). Then you can show the JPanel in a one of three ways.
JOptionPane.showMessageDialog(..). The idea would be to use the ready made OK/Cancel buttons of the option pane to tell the main app. whether to actually use image drawn. I (as a user) would tend to expect the Clear button to not be in the group of buttons that dismisses the dialog. Put that button in the panel you pass to the option pane.
A modal JDialog. Creating a dialog is more work than using an option pane, but also more versatile. For instance, if the panel you put into the dialog has a 'set size of drawing' option, it is easier to resize a dialog than an option pane.
Another card of a CardLayout. The two previous suggestions have the dis/advantage that they will block the app. and the browser until dismissed. This is good in that you can simply query the state of the drawing immediately after it is shown, confident that the user has finished drawing. But it is bad in that the user might have the dialog or option pane sitting on screen for 30 minutes, and the rest of the browser will be inaccessible to them in that time. By instead flipping to a card that shows the drawing panel, the browser is not blocked, and the app. can query the state of the drawing as soon as the user makes one of the OK/Cancel selections.

Libgdx : setting another screen, but still buttons from old screen active

In my libgdx game, I have 2 screens, menu and list.
When I click on a label in the menu screen, I do a setscreen(list).
The new screen shows up, and the menu screen alongwith its labels disappears.
But when I click on the same positions (from menu screen where the labels were, but of course those labels are not showing as I have changed screens) the click event responds. Why?
note:My list screen currently has not event handlers for any widget.
When switching screens do I need to do anything more than just setscreen(anotherscreen) to deactivate oldscreen?
I changed this :
I moved the input processor to the show() method of that screen using stage variable of that screen
public void show() {
...
Gdx.input.setInputProcessor(stage);
}
before I was setting this only in the constructor of the screen, so even If I was changing the screen, the input processor was still attached to the last created screen's stage
The answer above adiddnt work for me because I dont work with a InputProcessor in the 2nd screen.
My Solution was to set the Inputprocessor to null after setScreen.
Gdx.app.getApplicationListener().setScreen(new Screen());
Gdx.input.setInputProcessor(null);
Just broke my head over this. I changed screens in the show method but had Gdx.input.setInputProcessor(stage) called after that. The method still completed after the show method of the new screen ran and thus reverting back to the previous stage.
So make sure you call Gdx.input.setInputProcessor(stage) before you change screens or return after you change screens.

Categories