0

In this code the hitboxes are not sensing anything. The if statement which checks if 2 hitboxses intersect isn't doing the operations in it when the circle and rectangle are touching. How do i get this to work?

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.JFrame;

public class RunPaintGUI extends JFrame implements KeyListener {
    int run = 10;
    int x = 30;
    int y = 30;
    int score = 0;
    Random randomgenerator = new Random();
    int a = randomgenerator.nextInt(1220);
    int b = randomgenerator.nextInt(700);

    public static void main(String[] args) {

        RunPaintGUI RunPaintGUI = new RunPaintGUI();
    }

    public RunPaintGUI() {
        this.setSize(1275, 775);
        this.setResizable(false);
        this.setTitle("game");
        this.setVisible(true);
        this.addKeyListener(this);
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.fill3DRect(x, y, 60, 60, true);

        g.fillOval(a, b, 50, 50);

        g.drawString("score: " + score, 600, 50);

    }

    Rectangle2D hitbox = new Rectangle(x, y, 60, 60);
    Ellipse2D.Double hitbox1 = new Ellipse2D.Double(a, b, 50, 50);

    public void checkiftouching() {
        if (hitbox1.intersects(hitbox))
            ;

        System.out.println("you won");
        a = randomgenerator.nextInt(1220);
        b = randomgenerator.nextInt(720);
        repaint();
        score = score + 1;
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            x = x - run;
            hitbox.setFrame(x, getY(), getWidth(), getHeight());
            hitbox1.setFrame(a, b, 50, 50);
            repaint();

            // System.out.println(x);
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            x = x + run;
            hitbox.setFrame(x, getY(), getWidth(), getHeight());
            hitbox1.setFrame(a, b, 50, 50);
            repaint();

            // System.out.println(x);
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            y = y - run;
            hitbox.setFrame(getX(), y, getWidth(), getHeight());
            hitbox1.setFrame(a, b, 50, 50);
            repaint();

            // System.out.println(y);
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            y = y + run;
            hitbox.setFrame(getX(), y, getWidth(), getHeight());
            hitbox1.setFrame(a, b, 50, 50);
            repaint();

            // System.out.println(y);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41

1 Answers1

3

You will notice that

public void checkiftouching(){

is never called. Furthermore, this if statement has a semi-colon at the end, so it does very little.

if (hitbox1.intersects(hitbox));

Always follow your if, while, for, switches with complete brackets { .. }

If you use an IDE it should have highlighted these errors for you.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
javajon
  • 1,660
  • 16
  • 18
  • 1
    *"Always follow your if, while, for, switches with complete brackets `{ .. }`"* Excellent advice. – Andrew Thompson Jul 14 '14 at 07:17
  • 1
    In addition, the method name checkiftouching() might be a bit misleading, as technically the intersects method doesnt check for touching, just for intersection :) (Meaning that if just the borders overlay intersects(...) will return false) – flotothemoon Jul 14 '14 at 09:36