Movement with setOnKeyPressed and setOnKeyReleased
up vote
0
down vote
favorite
I wrote some code to control the movement of a small rectangle on the screen with JavaFX. Basically, I move the player smoothly in the game loop with these codes:
(P.S: player is my object)
player.setTranslateX(player.getTranslateX() + player.velocityX);
player.setTranslateY(player.getTranslateY() + player.velocityY);
And these are the key bindings:
myScene.setOnKeyPressed(e -> {
Thread t1;
t1 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = -1;
break;
case D:
player.velocityX = 1;
break;
case W:
player.velocityY = -1;
break;
case S:
player.velocityY = 1;
break;
}
}
});
t1.start();
})
Key releases:
myScene.setOnKeyReleased(e -> {
Thread t2;
t2 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = 0;
break;
case D:
player.velocityX = 0;
break;
case W:
player.velocityY = 0;
break;
case S:
player.velocityY = 0;
break;
}
}
});
t2.start();
})
It is working fine but there is a bug that when I press multiple keys at the same time and release one of them, the objects stops moving until I release my finger off the key and press the key again. Now I know it has something to do with the ambiguity concept but I've hit a brick wall and I'm open for ideas.
java javafx
|
show 1 more comment
up vote
0
down vote
favorite
I wrote some code to control the movement of a small rectangle on the screen with JavaFX. Basically, I move the player smoothly in the game loop with these codes:
(P.S: player is my object)
player.setTranslateX(player.getTranslateX() + player.velocityX);
player.setTranslateY(player.getTranslateY() + player.velocityY);
And these are the key bindings:
myScene.setOnKeyPressed(e -> {
Thread t1;
t1 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = -1;
break;
case D:
player.velocityX = 1;
break;
case W:
player.velocityY = -1;
break;
case S:
player.velocityY = 1;
break;
}
}
});
t1.start();
})
Key releases:
myScene.setOnKeyReleased(e -> {
Thread t2;
t2 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = 0;
break;
case D:
player.velocityX = 0;
break;
case W:
player.velocityY = 0;
break;
case S:
player.velocityY = 0;
break;
}
}
});
t2.start();
})
It is working fine but there is a bug that when I press multiple keys at the same time and release one of them, the objects stops moving until I release my finger off the key and press the key again. Now I know it has something to do with the ambiguity concept but I've hit a brick wall and I'm open for ideas.
java javafx
you should use an infinite loop to check for key presses and releases.
– preciousbetine
Nov 22 at 18:13
I tried creating an AnimationTimer but still the same.
– emir3333
Nov 22 at 18:49
I meant an infinite while loop
– preciousbetine
Nov 22 at 19:05
I created an infinite while(true) loop right after primaryStage.show() in the typical JavaFX @Override start() method and transferred everything in AnimationTimer handle method to there. But now it's stuck at game window. Where do you suggest i put the while loop?
– emir3333
Nov 22 at 19:40
put the run function in the loop
– preciousbetine
Nov 22 at 20:03
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I wrote some code to control the movement of a small rectangle on the screen with JavaFX. Basically, I move the player smoothly in the game loop with these codes:
(P.S: player is my object)
player.setTranslateX(player.getTranslateX() + player.velocityX);
player.setTranslateY(player.getTranslateY() + player.velocityY);
And these are the key bindings:
myScene.setOnKeyPressed(e -> {
Thread t1;
t1 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = -1;
break;
case D:
player.velocityX = 1;
break;
case W:
player.velocityY = -1;
break;
case S:
player.velocityY = 1;
break;
}
}
});
t1.start();
})
Key releases:
myScene.setOnKeyReleased(e -> {
Thread t2;
t2 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = 0;
break;
case D:
player.velocityX = 0;
break;
case W:
player.velocityY = 0;
break;
case S:
player.velocityY = 0;
break;
}
}
});
t2.start();
})
It is working fine but there is a bug that when I press multiple keys at the same time and release one of them, the objects stops moving until I release my finger off the key and press the key again. Now I know it has something to do with the ambiguity concept but I've hit a brick wall and I'm open for ideas.
java javafx
I wrote some code to control the movement of a small rectangle on the screen with JavaFX. Basically, I move the player smoothly in the game loop with these codes:
(P.S: player is my object)
player.setTranslateX(player.getTranslateX() + player.velocityX);
player.setTranslateY(player.getTranslateY() + player.velocityY);
And these are the key bindings:
myScene.setOnKeyPressed(e -> {
Thread t1;
t1 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = -1;
break;
case D:
player.velocityX = 1;
break;
case W:
player.velocityY = -1;
break;
case S:
player.velocityY = 1;
break;
}
}
});
t1.start();
})
Key releases:
myScene.setOnKeyReleased(e -> {
Thread t2;
t2 = new Thread(new Runnable() {
@Override
public void run() {
switch(e.getCode()) {
case A:
player.velocityX = 0;
break;
case D:
player.velocityX = 0;
break;
case W:
player.velocityY = 0;
break;
case S:
player.velocityY = 0;
break;
}
}
});
t2.start();
})
It is working fine but there is a bug that when I press multiple keys at the same time and release one of them, the objects stops moving until I release my finger off the key and press the key again. Now I know it has something to do with the ambiguity concept but I've hit a brick wall and I'm open for ideas.
java javafx
java javafx
edited Nov 22 at 22:45
Andrew Thompson
153k27163338
153k27163338
asked Nov 22 at 16:37
emir3333
14
14
you should use an infinite loop to check for key presses and releases.
– preciousbetine
Nov 22 at 18:13
I tried creating an AnimationTimer but still the same.
– emir3333
Nov 22 at 18:49
I meant an infinite while loop
– preciousbetine
Nov 22 at 19:05
I created an infinite while(true) loop right after primaryStage.show() in the typical JavaFX @Override start() method and transferred everything in AnimationTimer handle method to there. But now it's stuck at game window. Where do you suggest i put the while loop?
– emir3333
Nov 22 at 19:40
put the run function in the loop
– preciousbetine
Nov 22 at 20:03
|
show 1 more comment
you should use an infinite loop to check for key presses and releases.
– preciousbetine
Nov 22 at 18:13
I tried creating an AnimationTimer but still the same.
– emir3333
Nov 22 at 18:49
I meant an infinite while loop
– preciousbetine
Nov 22 at 19:05
I created an infinite while(true) loop right after primaryStage.show() in the typical JavaFX @Override start() method and transferred everything in AnimationTimer handle method to there. But now it's stuck at game window. Where do you suggest i put the while loop?
– emir3333
Nov 22 at 19:40
put the run function in the loop
– preciousbetine
Nov 22 at 20:03
you should use an infinite loop to check for key presses and releases.
– preciousbetine
Nov 22 at 18:13
you should use an infinite loop to check for key presses and releases.
– preciousbetine
Nov 22 at 18:13
I tried creating an AnimationTimer but still the same.
– emir3333
Nov 22 at 18:49
I tried creating an AnimationTimer but still the same.
– emir3333
Nov 22 at 18:49
I meant an infinite while loop
– preciousbetine
Nov 22 at 19:05
I meant an infinite while loop
– preciousbetine
Nov 22 at 19:05
I created an infinite while(true) loop right after primaryStage.show() in the typical JavaFX @Override start() method and transferred everything in AnimationTimer handle method to there. But now it's stuck at game window. Where do you suggest i put the while loop?
– emir3333
Nov 22 at 19:40
I created an infinite while(true) loop right after primaryStage.show() in the typical JavaFX @Override start() method and transferred everything in AnimationTimer handle method to there. But now it's stuck at game window. Where do you suggest i put the while loop?
– emir3333
Nov 22 at 19:40
put the run function in the loop
– preciousbetine
Nov 22 at 20:03
put the run function in the loop
– preciousbetine
Nov 22 at 20:03
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I've run into this problem before when writing a game just like you're writing.
After a bunch of debugging, it looked like the issue is actually related to the code you're writing, but rather to the library itself. It seems that if you hold down multiple keys, the buffer of received key presses gets "jammed" and it doesn't respond until you let go and press again.
One workaround for this on Mac is to run the following command in terminal:
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
This seemed to fix the behavior. For Windows, we weren't able to find anything to fix the issue.
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
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%2f53435166%2fmovement-with-setonkeypressed-and-setonkeyreleased%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
up vote
0
down vote
accepted
I've run into this problem before when writing a game just like you're writing.
After a bunch of debugging, it looked like the issue is actually related to the code you're writing, but rather to the library itself. It seems that if you hold down multiple keys, the buffer of received key presses gets "jammed" and it doesn't respond until you let go and press again.
One workaround for this on Mac is to run the following command in terminal:
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
This seemed to fix the behavior. For Windows, we weren't able to find anything to fix the issue.
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
add a comment |
up vote
0
down vote
accepted
I've run into this problem before when writing a game just like you're writing.
After a bunch of debugging, it looked like the issue is actually related to the code you're writing, but rather to the library itself. It seems that if you hold down multiple keys, the buffer of received key presses gets "jammed" and it doesn't respond until you let go and press again.
One workaround for this on Mac is to run the following command in terminal:
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
This seemed to fix the behavior. For Windows, we weren't able to find anything to fix the issue.
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I've run into this problem before when writing a game just like you're writing.
After a bunch of debugging, it looked like the issue is actually related to the code you're writing, but rather to the library itself. It seems that if you hold down multiple keys, the buffer of received key presses gets "jammed" and it doesn't respond until you let go and press again.
One workaround for this on Mac is to run the following command in terminal:
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
This seemed to fix the behavior. For Windows, we weren't able to find anything to fix the issue.
I've run into this problem before when writing a game just like you're writing.
After a bunch of debugging, it looked like the issue is actually related to the code you're writing, but rather to the library itself. It seems that if you hold down multiple keys, the buffer of received key presses gets "jammed" and it doesn't respond until you let go and press again.
One workaround for this on Mac is to run the following command in terminal:
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
This seemed to fix the behavior. For Windows, we weren't able to find anything to fix the issue.
answered Nov 22 at 17:31
Alex K
6,71983151
6,71983151
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
add a comment |
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
I figured out a way to transition from A to D by adding a flag like this: 1.) set flag to initially false. 2.) case A: if(flag2) { return; } player.velocityX = -1; break; /// But it doesn't work for transition from D to A though... Thanks for the response.
– emir3333
Nov 22 at 18:02
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%2f53435166%2fmovement-with-setonkeypressed-and-setonkeyreleased%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
you should use an infinite loop to check for key presses and releases.
– preciousbetine
Nov 22 at 18:13
I tried creating an AnimationTimer but still the same.
– emir3333
Nov 22 at 18:49
I meant an infinite while loop
– preciousbetine
Nov 22 at 19:05
I created an infinite while(true) loop right after primaryStage.show() in the typical JavaFX @Override start() method and transferred everything in AnimationTimer handle method to there. But now it's stuck at game window. Where do you suggest i put the while loop?
– emir3333
Nov 22 at 19:40
put the run function in the loop
– preciousbetine
Nov 22 at 20:03