infinite loop if pause java game












1














I have a part of my code:



while(_running){
// render screens
if( _input.escape) {
if( isPaused ) {
pauseDialog.setVisible(false);
remuse(); // set isPaused = false, _running = true and render screens
}
else {
pause(); // set isPaused = true and _running = false and render screens
pauseDialog.setVisible(true);
}
}
}


and _input like this



public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}


and pauseDialog



public class PauseDialog extends JDialog{
JButton b1,b2;
public PauseDialog() {
setLayout(new GridLayout(2, 1,8,8));
setSize(new Dimension(85, 180));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
b1 = new JButton("resume");
b2 = new JButton("exit");

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("resume");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("exit");
}
});
add(b1); add(b2);
}


but if i setvisible(true) for pauseDialog when escape key is released, variable _input.escape is always set true value, so dialog appears and disappears and appears again ... like an infinite loop. what should i do next? thanks.










share|improve this question
























  • try e.preventDefault() inside the keyPressed and keyReleased function...
    – 薛源少
    Nov 20 at 4:42








  • 1




    And just for the record: b1 and b2 are really bad names. Use names that tell the reader what the thing behind the variable is about.
    – GhostCat
    Nov 20 at 14:24










  • Change remuse to resume.
    – Jonathan Airey
    Nov 20 at 14:46












  • Also, what is _running? You could just write while(true).
    – Jonathan Airey
    Nov 20 at 14:47
















1














I have a part of my code:



while(_running){
// render screens
if( _input.escape) {
if( isPaused ) {
pauseDialog.setVisible(false);
remuse(); // set isPaused = false, _running = true and render screens
}
else {
pause(); // set isPaused = true and _running = false and render screens
pauseDialog.setVisible(true);
}
}
}


and _input like this



public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}


and pauseDialog



public class PauseDialog extends JDialog{
JButton b1,b2;
public PauseDialog() {
setLayout(new GridLayout(2, 1,8,8));
setSize(new Dimension(85, 180));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
b1 = new JButton("resume");
b2 = new JButton("exit");

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("resume");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("exit");
}
});
add(b1); add(b2);
}


but if i setvisible(true) for pauseDialog when escape key is released, variable _input.escape is always set true value, so dialog appears and disappears and appears again ... like an infinite loop. what should i do next? thanks.










share|improve this question
























  • try e.preventDefault() inside the keyPressed and keyReleased function...
    – 薛源少
    Nov 20 at 4:42








  • 1




    And just for the record: b1 and b2 are really bad names. Use names that tell the reader what the thing behind the variable is about.
    – GhostCat
    Nov 20 at 14:24










  • Change remuse to resume.
    – Jonathan Airey
    Nov 20 at 14:46












  • Also, what is _running? You could just write while(true).
    – Jonathan Airey
    Nov 20 at 14:47














1












1








1







I have a part of my code:



while(_running){
// render screens
if( _input.escape) {
if( isPaused ) {
pauseDialog.setVisible(false);
remuse(); // set isPaused = false, _running = true and render screens
}
else {
pause(); // set isPaused = true and _running = false and render screens
pauseDialog.setVisible(true);
}
}
}


and _input like this



public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}


and pauseDialog



public class PauseDialog extends JDialog{
JButton b1,b2;
public PauseDialog() {
setLayout(new GridLayout(2, 1,8,8));
setSize(new Dimension(85, 180));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
b1 = new JButton("resume");
b2 = new JButton("exit");

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("resume");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("exit");
}
});
add(b1); add(b2);
}


but if i setvisible(true) for pauseDialog when escape key is released, variable _input.escape is always set true value, so dialog appears and disappears and appears again ... like an infinite loop. what should i do next? thanks.










share|improve this question















I have a part of my code:



while(_running){
// render screens
if( _input.escape) {
if( isPaused ) {
pauseDialog.setVisible(false);
remuse(); // set isPaused = false, _running = true and render screens
}
else {
pause(); // set isPaused = true and _running = false and render screens
pauseDialog.setVisible(true);
}
}
}


and _input like this



public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}


and pauseDialog



public class PauseDialog extends JDialog{
JButton b1,b2;
public PauseDialog() {
setLayout(new GridLayout(2, 1,8,8));
setSize(new Dimension(85, 180));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
b1 = new JButton("resume");
b2 = new JButton("exit");

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("resume");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("exit");
}
});
add(b1); add(b2);
}


but if i setvisible(true) for pauseDialog when escape key is released, variable _input.escape is always set true value, so dialog appears and disappears and appears again ... like an infinite loop. what should i do next? thanks.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 14:23









Malandy

1269




1269










asked Nov 20 at 3:41









BlackW

112




112












  • try e.preventDefault() inside the keyPressed and keyReleased function...
    – 薛源少
    Nov 20 at 4:42








  • 1




    And just for the record: b1 and b2 are really bad names. Use names that tell the reader what the thing behind the variable is about.
    – GhostCat
    Nov 20 at 14:24










  • Change remuse to resume.
    – Jonathan Airey
    Nov 20 at 14:46












  • Also, what is _running? You could just write while(true).
    – Jonathan Airey
    Nov 20 at 14:47


















  • try e.preventDefault() inside the keyPressed and keyReleased function...
    – 薛源少
    Nov 20 at 4:42








  • 1




    And just for the record: b1 and b2 are really bad names. Use names that tell the reader what the thing behind the variable is about.
    – GhostCat
    Nov 20 at 14:24










  • Change remuse to resume.
    – Jonathan Airey
    Nov 20 at 14:46












  • Also, what is _running? You could just write while(true).
    – Jonathan Airey
    Nov 20 at 14:47
















try e.preventDefault() inside the keyPressed and keyReleased function...
– 薛源少
Nov 20 at 4:42






try e.preventDefault() inside the keyPressed and keyReleased function...
– 薛源少
Nov 20 at 4:42






1




1




And just for the record: b1 and b2 are really bad names. Use names that tell the reader what the thing behind the variable is about.
– GhostCat
Nov 20 at 14:24




And just for the record: b1 and b2 are really bad names. Use names that tell the reader what the thing behind the variable is about.
– GhostCat
Nov 20 at 14:24












Change remuse to resume.
– Jonathan Airey
Nov 20 at 14:46






Change remuse to resume.
– Jonathan Airey
Nov 20 at 14:46














Also, what is _running? You could just write while(true).
– Jonathan Airey
Nov 20 at 14:47




Also, what is _running? You could just write while(true).
– Jonathan Airey
Nov 20 at 14:47












1 Answer
1






active

oldest

votes


















0














Set "_input.escape" to False, after Escape is pressed?



Or does "_input.escape" need to be True for pausing to happen?



If so, that implies something's wrong, 'cause you shouldn't need to hold down Escape to keep the game paused?





It just seems you need a good logic system so that the dialog isn't being triggered more than once.



Maybe check if it's already triggered and don't trigger it again?






share|improve this answer





















  • Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
    – BlackW
    Nov 20 at 4:06










  • @BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
    – Malandy
    Nov 20 at 13:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385889%2finfinite-loop-if-pause-java-game%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Set "_input.escape" to False, after Escape is pressed?



Or does "_input.escape" need to be True for pausing to happen?



If so, that implies something's wrong, 'cause you shouldn't need to hold down Escape to keep the game paused?





It just seems you need a good logic system so that the dialog isn't being triggered more than once.



Maybe check if it's already triggered and don't trigger it again?






share|improve this answer





















  • Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
    – BlackW
    Nov 20 at 4:06










  • @BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
    – Malandy
    Nov 20 at 13:34
















0














Set "_input.escape" to False, after Escape is pressed?



Or does "_input.escape" need to be True for pausing to happen?



If so, that implies something's wrong, 'cause you shouldn't need to hold down Escape to keep the game paused?





It just seems you need a good logic system so that the dialog isn't being triggered more than once.



Maybe check if it's already triggered and don't trigger it again?






share|improve this answer





















  • Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
    – BlackW
    Nov 20 at 4:06










  • @BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
    – Malandy
    Nov 20 at 13:34














0












0








0






Set "_input.escape" to False, after Escape is pressed?



Or does "_input.escape" need to be True for pausing to happen?



If so, that implies something's wrong, 'cause you shouldn't need to hold down Escape to keep the game paused?





It just seems you need a good logic system so that the dialog isn't being triggered more than once.



Maybe check if it's already triggered and don't trigger it again?






share|improve this answer












Set "_input.escape" to False, after Escape is pressed?



Or does "_input.escape" need to be True for pausing to happen?



If so, that implies something's wrong, 'cause you shouldn't need to hold down Escape to keep the game paused?





It just seems you need a good logic system so that the dialog isn't being triggered more than once.



Maybe check if it's already triggered and don't trigger it again?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 3:50









Malandy

1269




1269












  • Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
    – BlackW
    Nov 20 at 4:06










  • @BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
    – Malandy
    Nov 20 at 13:34


















  • Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
    – BlackW
    Nov 20 at 4:06










  • @BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
    – Malandy
    Nov 20 at 13:34
















Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
– BlackW
Nov 20 at 4:06




Set true when pressed and false when releases and ive already set cooldown time between 2 times pause code which is called. But not working
– BlackW
Nov 20 at 4:06












@BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
– Malandy
Nov 20 at 13:34




@BlackW - Set so it changes state when pressed, instead of turning true when pressed and false when released...
– Malandy
Nov 20 at 13:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385889%2finfinite-loop-if-pause-java-game%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]