当"W or w"只按下一次时使block/Character移动"X px",按住"W and w"时不继续行走
Making block/Character move "X px" when "W or w" is only once when pressed, and not continue walking when "W and w" is held down
我想在按下 "W or w" 时让我的 Rectangle/Character 移动 "X px's" 但只有一次。并且在按住 "W and w" 时不继续移动。我已经尝试制作一个 "Key Released" 函数,其中的变量在按下 "W or w" 时会发生变化。但它并没有真正起作用,它只是让 Rectangle/Character 移动了 "X px's" 一次,然后就不会朝那个方向移动了。简而言之,我希望每次 "Click" 按钮时 Rectangle/Character 移动 50px,一步一步。而不是流畅的运动。谁能帮忙?
代码写在Java, Processing.
class Character {
float x = 0;
float y = 0;
//VV Movement VV
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
//terms, move forward/up, Y-axel.
y -= 50;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
//terms, move right, X-axel.
x -= 50;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
//terms, move backwards/down, Y-axel.
y += 50;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
//terms, move left, X-axel.
x += 50;
}
}
}
I want to make my Rectangle/Character move [...] but only once
添加一个keyPressed()
event and call move
from the event, but do not call it in draw()
.
注意 keyPressed
每次按下一个键都会调用一次。所以当按下一个键时,角色只会移动一次。但是由于操作系统可能会处理按键重复,因此必须存储最后按下的按键。如果在 keyReleased
出现之前再次按下相同的键,则必须跳过它。例如:
Character ch = new Character();
int lastKey = 0;
void keyPressed() {
if (lastKey != key) {
lastKey = key;
ch.move();
}
}
void keyReleased() {
lastKey = 0;
}
在示例中 ch
假定为您的 Character
实例。
我想在按下 "W or w" 时让我的 Rectangle/Character 移动 "X px's" 但只有一次。并且在按住 "W and w" 时不继续移动。我已经尝试制作一个 "Key Released" 函数,其中的变量在按下 "W or w" 时会发生变化。但它并没有真正起作用,它只是让 Rectangle/Character 移动了 "X px's" 一次,然后就不会朝那个方向移动了。简而言之,我希望每次 "Click" 按钮时 Rectangle/Character 移动 50px,一步一步。而不是流畅的运动。谁能帮忙? 代码写在Java, Processing.
class Character {
float x = 0;
float y = 0;
//VV Movement VV
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
//terms, move forward/up, Y-axel.
y -= 50;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
//terms, move right, X-axel.
x -= 50;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
//terms, move backwards/down, Y-axel.
y += 50;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
//terms, move left, X-axel.
x += 50;
}
}
}
I want to make my Rectangle/Character move [...] but only once
添加一个keyPressed()
event and call move
from the event, but do not call it in draw()
.
注意 keyPressed
每次按下一个键都会调用一次。所以当按下一个键时,角色只会移动一次。但是由于操作系统可能会处理按键重复,因此必须存储最后按下的按键。如果在 keyReleased
出现之前再次按下相同的键,则必须跳过它。例如:
Character ch = new Character();
int lastKey = 0;
void keyPressed() {
if (lastKey != key) {
lastKey = key;
ch.move();
}
}
void keyReleased() {
lastKey = 0;
}
在示例中 ch
假定为您的 Character
实例。