简体   繁体   中英

Character falls off the edge of the screen

So I'm making a game, and the user controls a rectangle that moves left to right along a box at the bottom, but it falls of the edges and I don't know how to stop this, help would be appreciated. I have tried many things to stop this but nothing has worked and I really need some help to keep the rectangle actually on the screen.

public Rectangle character;
public Rectangle bottomBox;

public int charW = 100;
public int charH = 15;

public boolean right = false;
public boolean left = false;
public boolean up = false;
public boolean down = false;
public boolean mouseActive = false;

public Point mouse;

public Keying(Display f, Images i){
    character = new Rectangle(180, 180, charW, charH); 
    bottomBox = new Rectangle (0, 350, 9000, 30);

    f.addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_D){
                mouseActive = false;
                right = true;
                character.x += 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_A){
                mouseActive = false;
                left = true;
                character.x -= 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_M){
                mouseActive = false;
            }
        }

        public void keyReleased(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_D){
                right = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_A){
                left = false;
            }
        }
    });

    f.addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent e){
            int mouseX = e.getX();
            int mouseY = e.getY();
            mouse = new Point(mouseX, mouseY); 
            if(mouseActive && character.x != Main.w - charW){
                character.x = mouse.x;
                character.y = mouse.y;
            }
            repaint();
        }           
    });

    f.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
        mouse = new Point (e.getX(), e.getY());

        if(e.getButton() == MouseEvent.NOBUTTON){
            character.x = mouse.x;
            character.y = mouse.y;
        }
        }
    });
}    

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Point pt1 = new Point(character.x, character.y + character.height);
    if(!bottomBox.contains(pt1) && !mouseActive){
        character.y++;         
    }

    this.setBackground(Color.YELLOW);
    {g.setColor(Color.BLACK);
    g.fillRect(character.x, character.y, character.width, character.height);}
    {g.setColor(Color.darkGray);
    g.fillRect(bottomBox.x, bottomBox.y, bottomBox.width, bottomBox.height);

    if(right && character.x != Main.w - charW){
        character.x += 1;
    }
    if(left && character.x != 0){
        character.x -= 1;
    }
    repaint();   
}

} }

I really need some help to keep the rectangle actually on the screen.

Well you need to do some basic logic check to see if the rectangle can be painted within the bounds of the panel.

You now the location of the rectangle and the width so just check if the sum is not greater than the width of the panel.

For example you can check out the logic found in Motion Using the Keyboard . All the code examples contain a move(...) method which does the boundary checking.

Also, note from the example that your painting code should NOT be calculation the location of the characters. A painting method should only paint the character based on the current properties of the character. Only a user action should change a property of the character because you can't control when the painting method is called.

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