[英]How do I stop an animation once a certain barrier has been reached?
Today's question rattling my mind is such. 今天困扰我的问题就是这样。 I'm creating a program that allow the user to jump and move left or right, to problem is that whenever I try to jump the program freezes... The idea behind my code is simple, whenever the users presses space(jump) the rectangle "jumps" up and if the y of the rectangle is within a certain height above the obstacle(in this case a rectangle with dimmensions brickx,bricky,brickw, and brickh is the obstacle) then the animation is supposed to stop and the rectangle is supposed to wait for the next command while in place on top of the obstacle. 我正在创建一个允许用户向左或向右移动的程序,问题是,每当我尝试跳转时,程序都会冻结...代码的想法很简单,每当用户按下空格键时,矩形“跳”起来,如果矩形的y在障碍物上方一定高度之内(在这种情况下,尺寸为brickx,bricky,brickw和brickh的矩形是障碍物),则动画应该停止并且该矩形应该在障碍物上方时等待下一个命令。 To do this a method stayOnBrick is called during each iteration of the while loop and checks; 为此,在while循环的每次迭代期间都会调用stayOnBrick方法并进行检查; during the jump, if y is within the needed range, and if y is it then sets the boolean jump = true which should break the loop on the next iteration as well as setting y to the needed value. 在跳转期间,如果y在所需范围内,并且如果y为y,则将其设置为布尔跳转= true,这将中断下一次迭代的循环并将y设置为所需值。 But when space is pressed, nothing happens, or should I say the program freezes. 但是当按下空格时,什么也没有发生,或者我应该说程序死机了。 Thoughts? 有什么想法吗?
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyTest extends Core implements KeyListener{
Window w;
public int x,y;
boolean jump = true;
public int brickx, bricky, brickw, brickh;
public static void main(String args[]){
new KeyTest().run();
}
private String mess = "";
//init also called init from superclass
public void init(){
super.init();
Window w = s.getFullScreenWindow();
w.setFocusTraversalKeysEnabled(false);
w.addKeyListener(this);
y = s.getHeight()-15;
mess = "Press esc to exit";
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
e.consume();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
stop();
}else if(keyCode == KeyEvent.VK_SPACE){
mess = "Pressed: " + KeyEvent.getKeyText(keyCode);
while(y>s.getHeight()-200){
stayOnBrick();
if(jump==false){break;}
else{ try{
y-=20;
w.repaint();
Thread.sleep(40);
}catch(Exception jumpError){
System.out.println("Error in Jumping");
}
}
while(y<s.getHeight()-15){
stayOnBrick();
if(jump==false){
w.repaint();
break;}
else{
try{
y+=20;
Thread.sleep(40);
w.repaint();
}catch(Exception jumpError){
System.out.println("Error in Jumping");
}
}
}
}
}
else if(keyCode == e.VK_RIGHT){
if(x>=s.getWidth()-30){x=s.getWidth()-30;}
x+=20;
w.repaint();
}
else if(keyCode == e.VK_LEFT){
if(x<=0){x=0;}
x-=20;
w.repaint();
}
e.consume();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
mess = "Released: " + KeyEvent.getKeyText(keyCode);
jump = true;
e.consume();
}
@Override
public synchronized void draw(Graphics2D g){
brickx=0; bricky=s.getHeight()-100; brickw=300; brickh=20;
Window w = s.getFullScreenWindow();
g.setColor(w.getBackground());
g.fillRect(0, 0, s.getWidth(), s.getHeight());
g.setColor(w.getForeground());
g.fillRect(x, y, 30, 15);
g.drawString(mess, 30, 30);
g.setColor(Color.BLUE);
g.fillRect(brickx, bricky, brickw, brickh);
}
public void stayOnBrick(){
if(y<bricky && y>bricky-30){
y=bricky-15;
jump = false;
}
else{jump = true;}
}
}
whenever I try to jump the program freezes 每当我尝试跳转时程序冻结
I'd start by having a look at Concurrency in Swing for the reason for the problem. 由于问题的原因,我首先来看看Swing中的Concurrency 。
I'd suggest having a look at How to use Swing Timers for a possible solution. 我建议看看如何使用Swing计时器作为可能的解决方案。
I'd also recommend having a look at How to Use Key Bindings to solve the focus related issues with KeyListener
我还建议您看看如何使用键绑定来解决与KeyListener
相关的焦点问题
And you might find How to make sprite jump in java? 您可能会发现如何在Java中使Sprite跳转? and JApplet creates a ball that bounces and gets progressively less high in Java helpful JApplet创建了一个反弹的球,并逐渐降低了Java的高度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.