使用相交方法的StackOverflowError

时间:2018-12-03 02:18:51

标签: java

当我运行代码时,将弹出一个空白窗口,并且ig得到一个StackOverflowError。我已经弄乱了代码,所以我知道它与intersect方法有关。我试图在游戏编程中添加冲突。谁能告诉我如何解决?

这是我得到的错误:

  

线程“ RainThread”中的异常java.lang.StackOverflowError位于   UserInterface.Main $ Rectangle.intersects(Main.java:66)在   UserInterface.Main $ Rectangle.intersects(Main.java:66)在   UserInterface.Main $ Rectangle.intersects(Main.java:66)在   UserInterface.Main $ Rectangle.intersects(Main.java:66)       在

package UserInterface;
import UserInterface.Drops;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.*;

public class Main extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;
    public Thread thread;
    public Rectangle character = new Rectangle(450,550, 50, 70);
    public Rectangle char2 = new Rectangle(600, 550, 50, 70);
    public Rectangle bottomBox = new Rectangle(0, 620, 900, 100 );      
    public int dropnumber=70;
    public Drops[] drops = new Drops[dropnumber];           
    public boolean running = false;     

    class Rectangle {

        public Rectangle(int xCoord, int yCoord, int rectWidth, int 
rectHeight) {
            x = xCoord;
            y = yCoord;
            width = rectWidth;
            height = rectHeight;            
        }

        public int x, y, width, height; 


        public void updateRight() {
            if(x < Main.w-55)
            x++;        
        }

        public void updateLeft() {
            if(x > 0)
            x--;        
        }

        public void draw (Graphics g) {
            g.setColor(Color.red);
            g.fillRect(character.x, character.y, character.width, 
 character.height);
        }

        public void draw2 (Graphics g) {
            g.setColor(Color.yellow);
            g.fillRect(char2.x, char2.y, char2.width, char2.height);
        }

        public void draw3 (Graphics g) {
            g.setColor(Color.gray);
            g.fillRect(bottomBox.x, bottomBox.y, bottomBox.width, 
 bottomBox.height);
        }
        public int getRectX() {
            return x;
        }

        public int getRectY() {
            return y;
        }           

        public boolean intersects(Rectangle r) {
            boolean state = false;
            if (character.intersects(r)){
                state = true;
            }
            return state;
        }


        }

    public Main() {
        for (int i =0; i < dropnumber;i++) {
            drops[i] = new Drops();
        }   

        addKeyListener(new Keying());       
    }

    public void tick() {            
        if(Keying.isKeyDown(KeyEvent.VK_A) || 
Keying.isKeyDown(KeyEvent.VK_LEFT)) {
            character.updateLeft();
            //System.out.println(character.getRectX());
        }
        if(Keying.isKeyDown(KeyEvent.VK_D) || 
Keying.isKeyDown(KeyEvent.VK_RIGHT)) {
            character.updateRight();
            //System.out.println(character.getRectX());
        }           
    }

    public void start() {
        if (running)
            return;
        thread = new Thread (this, "RainThread");
        thread.start();
        running = true;

    }

    public synchronized void stop() {
        if (!running)
            return;
        running = false;
        try {
            thread.join();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        requestFocus();
        while (running) {
            update();
            render();
            tick(); 
        if(character.intersects(char2)) {
            System.out.println("Intersection!");
        }
        }
        stop();
    }

    public void update() {
        for (int i = 0; i < dropnumber; i++) {
            drops[i].update();
        }


    }           

    private void render() {         
        BufferStrategy bs = getBufferStrategy();
        if(bs == null) {
            createBufferStrategy(3);
            return;     
            }               
        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0, 0, w, h);

        for (int i = 0; i < dropnumber; i++) {
            drops[i].draw(g);
        }

        bottomBox.draw3(g);         
        character.draw(g);          
        char2.draw2(g);

        g.dispose();
        bs.show();

    }       


public static int w = 900, h = 700;
public static void main (String[] args) {       
    Main main = new Main();
    JFrame frame = new JFrame("Dodging Game");
    frame.add(main);
    frame.setSize(w, h);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    main.start();
}}

如果需要,这是Keying类。

package UserInterface;

import java.awt.event.*;

public class Keying extends KeyAdapter implements KeyListener {

private static final boolean[] keys = new boolean [256];    

@Override
public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;        
}

@Override
public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}

public static boolean isKeyDown(int keyCode) {
    return keys[keyCode];
}
}

0 个答案:

没有答案