简体   繁体   中英

Why does my move() method not keep the ball in bounds?

Question: So I have created a program with a Window class that creates a JFrame and adds a JPanel from my DrawStuff class onto it. The DrawStuff class creates a ball that (is supposed to) bounce around the screen and when it hits the JFrame boundaries, change direction. The ball moves but for some reason the checkbounds part of my move method does not work. Any help would be greatly appreciated. My goal is to keep the ball in bounds.

Code from DrawStuff class :

public class Drawstuff extends JPanel {
    private int x = 0;
    private int y = 0;
    private int dx, dy=0;

    public  Drawstuff(){
        x = 300;
        y = 250;
        dx = 0;
        dy = 0;
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        this.setBackground(Color.BLACK);
        g2d.setColor(Color.RED);
        g2d.fillOval(x,y,50,50);    
    }
    public void move(){

        if (x < 600){
            dx = 1;
        }
        if (y < 500){
            dy = 1; 
        }
        if (x > 600){
            dx = -1; 
        }
        if (y >500){
            dy = -1;
        }
        x += dx;
        y += dy;
    }

}

Simple "GameLoop" from Window Class (if needed)

 while (true){
            stuff.repaint();
            stuff.move();
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Your move is wrong. The logic of it is false. You want the ball to bounce so make its movement reverse when it hits a wall. What you've done is : if it is outside get it inside and when inside try to get it outside! Change to:

   public void move(){
        if (x < 0) { // left bound
            dx = 1;  // now move right
        }
        if (y < 0){  // upper bound
            dy = 1;  // now move down
        }
        if (x > 600){// right bound
            dx = -1; // now move left
        }
        if (y >500){ // lower bound
            dy = -1; // now move up
        }
        x += dx;
        y += dy;
    }

For future use, I can suggest you to do it the following way :

   public void move(){
        if (x < 0) {   // left bound
            dx = -dx;  // now move right, same speed
            x=-x;      // bounce inside
        }
        if (y < 0){    // upper bound
            dy = -dy;  // now move down, same speed
            y=-y;      // bounce inside
        }
        if (x > 600){      // right bound
            dx = -dy;      // now move left, same speed
            x=600-(x-600); // bounce inside
        }
        if (y >500){       // lower bound
            dy = -dy;      // now move up, same speed
            y=500-(y-500); // bounce inside
        }
        x += dx;
        y += dy;
    }

So you can now use any vector speed you want (at least reasonable ones). Vector coordinates are reversed accordingly to the hit, and the ball is relocated inside the bounds.

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