简体   繁体   English

一旦达到特定的障碍,如何停止动画?

[英]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.

相关问题 达到某个值后如何触发声音 - How to trigger a sound once after a certain value has been reached 达到一定分数后如何更改等级? (Java、NetBeans) - How do I change my level after a certain score has been reached? (Java, NetBeans) 满足特定条件后,如何在世界/级别之间切换? - How do I switch between worlds/levels once certain criteria has been met? 如何让我的 Java 程序在读取到某个频率后立即停止捕获音频? - How do I make my Java program stop capturing audio as soon as a certain frequency has been read? 变量设置为false后,如何停止线程? - How to stop threads once variable has been set to false? 一旦线程被中断,如何完全停止线程? - How to completely stop a thread once it has been interrupted? 一旦循环到列表中的所有记录,如何停止循环 - How to stop for loop once it has been looped to all records in a list 一旦在 Java 中执行了程序,如何让 for 循环停止运行并继续执行下一个方法? - How can I get the for loop to stop to stop running and proceed to the next methid once the program has been executed in Java? 在Java游戏中达到分数后,重新生活 - Adding a new life once a score has been reached in a Java game 我如何知道该应用是否已从 Google Play 下载过一次 - How do I know if the app has been downloaded from Google Play once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM