简体   繁体   中英

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. To do this a method stayOnBrick is called during each iteration of the while loop and checks; 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. 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.

I'd suggest having a look at How to use Swing Timers for a possible solution.

I'd also recommend having a look at How to Use Key Bindings to solve the focus related issues with KeyListener

And you might find How to make sprite jump in java? and JApplet creates a ball that bounces and gets progressively less high in Java helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM