英文:
How would I stop my shape from moving using keypressed?
问题
如何使我的形状在每次按下空格键时停止,并在再次按下空格键时移动。
英文:
How would I make my shape stop every time I press the spacebar and move when I press the spacebar again.
答案1
得分: 2
为了暂停和恢复更新位置,您需要创建一个单独的变量来跟踪此状态更改:在这种情况下,布尔值非常适用。
例如:
boolean shouldSquareUpdate = true;
您可以使用 keyPressed()
来监听 Processing 中的按键更改:
void draw(){
}
void keyPressed(){
println("key: " + key + " keyCode: " + keyCode);
}
接下来是切换布尔值的部分:
- 如果值为
true
,则将其设置为false
- 如果值为
false
,则将其设置为true
例如:
void keyPressed(){
// 切换布尔值(如果为 true,则将其设置为 false,如果为 false,则将其设置为 true)
if(shouldSquareUpdate == true){
shouldSquareUpdate = false;
}else{
shouldSquareUpdate = true;
}
}
这可以更优雅地使用 逻辑 NOT 运算符 来解决,它本质上是翻转布尔值的状态:
void keyPressed(){
// 切换布尔值(如果为 true,则将其设置为 false,如果为 false,则将其设置为 true)
shouldSquareUpdate = !shouldSquareUpdate;
}
最后,您可以使用这个条件来确定是否应更新 y
值(或不更新):
// 仅在值为 true 时更新
if(shouldSquareUpdate){
y += dy;
}
完整的示例代码如下:
int x = 0;
int y = 0;
int dy = 1;
boolean shouldSquareUpdate = true;
void setup() {
size(1280, 720);
surface.setResizable(true);
}
void draw() {
background(240, 240, 240);
fill(255, 147, 79);
rect(x, y, 90, 90);
// 仅在值为 true 时更新
if(shouldSquareUpdate){
y += dy;
}
if(y + 90 > height || y < 0) {
dy *= -1;
}
}
void keyPressed(){
// 切换布尔值(如果为 true,则将其设置为 false,如果为 false,则将其设置为 true)
shouldSquareUpdate = !shouldSquareUpdate;
}
关于
> 如何使我的正方形以8等步长移动?
这只需要简单地更新 dy
值,您目前将其设置为1(每帧一个像素),使其成为总行程距离(高度)的 1/8 即可:
例如:
// 在 setup() 中
dy = height / 8;
请注意,720 / 8 = 90 像素每帧,每秒 60 帧,这将是 5400 像素每秒:速度太快,无法清晰看到运动。
您可以使用相同的原理来使方框以32等步长移动吗?
例如:
void setup() {
size(1280, 720);
surface.setResizable(true);
// 使正方形以32等步长移动:每次增加总高度的 1/32
dy = height / 32;
}
英文:
To pause and resume updating the position you would need to create a separate variable to keep track of this state change: a boolean value is perfect in this case.
e.g.
boolean shouldSquareUpdate = true;
You can use keyPressed()
to listen for key changes in Processing:
void draw(){
}
void keyPressed(){
println("key: " + key + " keyCode: " + keyCode);
}
The next part is toggling the boolean value:
- if the value was
true
, set it tofalse
- if it was
false
, set it totrue
e.g.
void keyPressed(){
// toggle boolean (if true, set it to false, if false, set it to true)
if(shouldSquareUpdate == true){
shouldSquareUpdate = false;
}else{
shouldSquareUpdate = true;
}
}
This can be more elegantly be solved with the logical NOT operator, which essentially flips the state of the boolean:
void keyPressed(){
// toggle boolean (if true, set it to false, if false, set it to true)
shouldSquareUpdate = !shouldSquareUpdate;
}
Finally, you would use this condition to determine if the y
value should be updated (or not):
// update only if value is true
if(shouldSquareUpdate){
y += dy;
}
The full sketch would like so:
int x = 0;
int y = 0;
int dy = 1;
boolean shouldSquareUpdate = true;
void setup() {
size(1280, 720);
surface.setResizable(true);
}
void draw() {
background(240, 240, 240);
fill(255, 147, 79);
rect(x, y, 90, 90);
// update only if value is true
if(shouldSquareUpdate){
y += dy;
}
if(y + 90 > height || y < 0) {
dy *= -1;
}
}
void keyPressed(){
// toggle boolean (if true, set it to false, if false, set it to true)
shouldSquareUpdate = !shouldSquareUpdate;
}
Regarding
> how would I make my square move in 8 equal steps?
That should be a matter of simply updating the dy
value which you currently set a 1 (pixel per frame) to be an 8th of the total travel distance (height):
e.g.
// in setup()
dy = height / 8;
Bare in mind that 720 / 8 = 90 pixels per frame, with 60 frames a second that would be 5400 pixels per second: waaay to fast for the motion be legible.
You can use the same principle to maybe move the box in 32 equal steps ?
e.g.
void setup() {
size(1280, 720);
surface.setResizable(true);
// make square move in 32 equal steps: increment 32th of the total height
dy = height / 32;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论