infinite loop if pause java game
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
add a comment |
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
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
Changeremuse
toresume
.
– Jonathan Airey
Nov 20 at 14:46
Also, what is_running
? You could just writewhile(true)
.
– Jonathan Airey
Nov 20 at 14:47
add a comment |
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
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
java
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
Changeremuse
toresume
.
– Jonathan Airey
Nov 20 at 14:46
Also, what is_running
? You could just writewhile(true)
.
– Jonathan Airey
Nov 20 at 14:47
add a comment |
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
Changeremuse
toresume
.
– Jonathan Airey
Nov 20 at 14:46
Also, what is_running
? You could just writewhile(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
add a comment |
1 Answer
1
active
oldest
votes
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?
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
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
add a comment |
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?
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
add a comment |
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?
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?
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
toresume
.– Jonathan Airey
Nov 20 at 14:46
Also, what is
_running
? You could just writewhile(true)
.– Jonathan Airey
Nov 20 at 14:47