I am trying to have one Button for pause and resume game background music on Jpanel form, what I have done below is just resets/repeats the sound from the beginning rather than pause and play. and I have look at few other examples here and tried to implement, same thing happened. or just paused it. any idea what to add to it to make it function? thanks
Boolean isPaused = false;
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
if (isPaused = false ) {
Game.gameMusic.pause();
isPaused = true;
}
else {
Game.gameMusic.resume();
isPaused = true;
}
}
if (isPaused = false ) { is an assignment, not an evaluation, you should be using ==
that will become
Boolean isPaused = false;
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
if (!isPaused ) {
Game.gameMusic.pause();
jButton6.setText("resume");
}
else {
Game.gameMusic.resume();
jButton6.setText("pause");
}
isPaused = !isPaused;
}
Related
So I have the main game while loop under the condition running, so when running is false the game stops running, this is threaded.
Here is the method for when I press the P key on keyboard to pause the game, I've tried a few things and nothing seems to work, my goal is to make it so that when I press P the game is paused (which it does) thereafter I can press P again to unpause.
boolean pause = false;
public void keyPressed(KeyEvent e) {
pause = true;
int key = e.getKeyCode();
if (key == KeyEvent.VK_P) {
if(pause) {
running = false;
} else {
running = true;
pause = false;
}
}
Since you have pause = true as the first line of keyPressed, pressing P will always pause the game. Try this instead:
boolean pause = false;
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_P) {
pause = !pause;
running = !pause;
}
}
So i've got an audio file that i'd like to play within a thread, this is where i've left the comment // Tried playing audio here . But the even with the logic to make sure that it only plays once no audio is being played. Instead it seems like it's forcing the application to skip frames instead causing the game to have a few glitches because of the inconsistent frame rate.
Here's the code which i've been using.
while (isRunning) {
// Go back to the if statement and check if the surface still isn't valid
if (!mySurfaceHolder.getSurface().isValid())
continue;
// Lock the canvas for editing, make changes and unlock with new changes
final Canvas canvas = mySurfaceHolder.lockCanvas();
canvas.drawRect(0,0,canvas.getWidth(), canvas.getHeight(), pWhite);
// Create the block to check the answer
answerObject = new AnswerObject(canvas);
animalObject.move(canvas, tilt);
if(answerObject.onCollision(animalObject) != null){
// Inside of box else is outside of box
if (answerObject.onCollision(animalObject)) {
// If the animal is part of that enviroment
if (verifyEnviroment(enviromentObject.getAnimals())){
isCorrect = true;
} else {
isCorrect = false;
}
} else {
// If the animal is part of that enviroment
if (!verifyEnviroment(enviromentObject.getAnimals())){
// Tried playing audio here
isCorrect = true;
} else {
// Tried playing audio here
isCorrect = false;
}
}
gameActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (isCorrect){
scoreObject.setScore(10);
} else {
healthObject.setHealth(10);
healthObject.setHealthImgs(healthObject.getHealth());
if (healthObject.getHealth() <= 0){
isRunning = false;
}
}
resetGameObjects();
}
});
}
mySurfaceHolder.unlockCanvasAndPost(canvas);
}
I hope I can describe my problem in the right way. I am programming a simple Twin-Stick Shooter at the moment so I implemented a KeyInputHandler calss that implements KeyListener. But when the player pressed 2 buttons at the same time I ran into a problem. It took always a second before the player actually moves. As far as I know that is because of the key repeat. I might be wrong. I googled a bit and found a possible solution here:
https://gamedev.stackexchange.com/questions/51623/smoother-controls?rq=1
The people suggested to not change the x- and y-velocity of the player on key press but instead flag the direction when the player presses a certain button and handle the movement in a different method. So I created the boolean variables and activated them when the button is pressed:
public class KeyInputHandler implements KeyListener{
private Game game;
// checks if A key is down
public boolean left_key_down = false;
// checks if D key is down
public boolean right_key_down = false;
// checks if W key is down
public boolean up_key_down = false;
// checks if S key is down
public boolean down_key_down = false;
public KeyInputHandler(Game game) {
this.game = game;
}
public void keyPressed(KeyEvent e) {
int keys = e.getKeyCode();
if (keys == KeyEvent.VK_D) {
right_key_down = true;
} else if (keys == KeyEvent.VK_A) {
left_key_down = true;
} else if (keys == KeyEvent.VK_W) {
up_key_down = true;
} else if (keys == KeyEvent.VK_S) {
down_key_down = true;
}
}
public void keyReleased(KeyEvent e) {
int keys = e.getKeyCode();
if (keys == KeyEvent.VK_D) {
right_key_down = false;
} else if (keys == KeyEvent.VK_A) {
left_key_down = false;
} else if (keys == KeyEvent.VK_W) {
up_key_down = false;
} else if (keys == KeyEvent.VK_S) {
down_key_down = false;
}
}
public void keyTyped(KeyEvent e) {
}
public void tick() {
if (left_key_down) {
game.p.setVelX(-1);
} else if (right_key_down) {
game.p.setVelX(1);
} else if (up_key_down) {
game.p.setVelY(-1);
} else if (down_key_down) {
game.p.setVelY(1);
} else {
game.p.setVelY(0);
game.p.setVelX(0);
}
}
}
(The tick() method is called in the main game loop)
The problem is now that there is no delay anymore (yay), but the things get strange when I press 2 buttons together. Example:
I press the left Button
I press the down Button
I let the down Button go -> Objects still goes down-left
I press up -> Object goes up-left
But when I hold Right and then als hold Down it is just moving down.
I also checked the boolean variables and they are getting refreshed correctly.
This is kinda weird and I am not really sure how to handle this and what exactly is causing this behaviour. I have the assumption that it has something to do with the order in which the cooleans are checked. Maybe someon can help me on this matter. I would greatly appreciate it. :)
The problem why things are getting strange in multiple key presses together is that you are using if else's in checking the booleans. Separete all the boolean checks in tick() to own individual ifs. If any one of the buttons are being pressed constantly, the code never goes to the last else-block where VelX and VelY are reset to zero.
Change the code in tick method to something like this:
//Set 'velocity' to zero first
game.p.setVelY(0);
game.p.setVelX(0);
if (left_key_down) {
game.p.setVelX(-1);
}
if (right_key_down) {
game.p.setVelX(1);
}
if (up_key_down) {
game.p.setVelY(-1);
}
if (down_key_down) {
game.p.setVelY(1);
}
So I have been trying to disable/enable a jPanel within a jTabbedPane (on Netbeans) using a simple boolean and the boolean always gets stuck on false. The disabling/enabling of the jPanel within the jTabbedPane is triggered by the click of a button. Here is an excerpt of my code:
package programming.club;
/**
*
* #author RandomGuy
*/
public class GranadaProgrammingClubUI extends javax.swing.JFrame {
public static boolean allowNavigation = true;
/**
* Creates new form ProgrammingClubUI
*/
public ProgrammingClubUI() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (allowNavigation == false) {
jTabbedPane1.setEnabledAt(0, true);
allowNavigation = true;
}
if (allowNavigation == true) {
jTabbedPane1.setEnabledAt(0, false);
allowNavigation = false;
}
System.out.println(allowNavigation);
}
I put the System.out.println(allowNavigation); just to check what the value of allowNavigation was, and every time I click the button, the value just stays as false, but it should be switching, and changing the status of the jPanel, enabled or disabled! (I think) I really can't figure out what's wrong here. Thank you for your time.
i think problem is
if (allowNavigation == false) {
jTabbedPane1.setEnabledAt(0, true);
allowNavigation = true;
}
if (allowNavigation == true) {
jTabbedPane1.setEnabledAt(0, false);
allowNavigation = false;
}
you should use
if (allowNavigation == false) {
jTabbedPane1.setEnabledAt(0, true);
allowNavigation = true;
}
else if (allowNavigation == true) {
jTabbedPane1.setEnabledAt(0, false);
allowNavigation = false;
}
because if allownavigation false it will go to first if and make it true and then go to second if and make it false
and also good boolean check is
if (!allowNavigation) {
jTabbedPane1.setEnabledAt(0, true);
allowNavigation = true;
}
else if (allowNavigation) {
jTabbedPane1.setEnabledAt(0, false);
allowNavigation = false;
}
I have a screen and have created a new multiplexer and set an input processor.
How can I override keyDown on my screen?
public class Screen implements Screen, TextInputListener
{
...stuff...
public Screen(Game game)
{
multiplexer = new InputMultiplexer();
multiplexer.addProcessor(mySystem);
multiplexer.addProcessor(aStage);
Gdx.input.setInputProcessor(multiplexer);
}
...more stuff....
}
Thanks
To override KeyDown you need to implement InputProcessor.
For example if you want to override the Android Back button and the Backspace button on PC you can use a class like this.
public class ScreenInputHandler implements InputProcessor {
#Override
public boolean keyDown(int keycode) {
if(keycode == Keys.BACK || keycode == Keys.BACKSPACE){
...code here
}
return false;
}
}
Remember add the input handler to your InputMultiplexer.
This is how I ended up solving it:
InputProcessor backProcessor = new InputAdapter()
{
#Override
public boolean keyDown(int keycode)
{
//do stuff
return false;
}
};
multiplexer.addProcessor(backProcessor);