2016-05-19 24 views
-1

Ich bin neu in Java Grafiken und ich arbeite gerade an einem Spiel. Im Wesentlichen gibt es aufsteigende Blasen, und der Benutzer muss sie platzen lassen, indem er die Maus über sie bewegt.Wie Instanzvariablen für Objekte in einem Array zurückgegeben werden

Ich habe bereits die Blasen gemacht, aber jetzt für die Kollisionserkennung muss ich in der Lage sein, das x und die Koordinaten sowie den Durchmesser des Kreises zu finden. Wie kann ich die Instanzvariablen für jede Blase (aus dem Bubbles-Objekt-Array) zurückgeben? Sie können meinen Code unten sehen. Wenn Sie Codierungsfehler finden, lassen Sie es mich wissen.

Spielklasse:

public class Game extends JPanel{ 

    public static final int WINDOW_WIDTH = 600; 
    public static final int WINDOW_HEIGHT = 400; 

    private boolean insideCircle = false; 
    private static boolean ifPaused = false; 
    private static JPanel mainPanel; 
    private static JLabel statusbar; 
    private static int clicks = 0; 
    //Creates a Bubbles object Array 
    Bubbles[] BubblesArray = new Bubbles[5]; 

    public Game() { 

    //initializes bubble objects 
    for (int i = 0; i < BubblesArray.length; i++) 
     BubblesArray[i] = new Bubbles(); 
    } 

public void paint(Graphics graphics) { 

    //makes background white 
    graphics.setColor(Color.WHITE); 
    graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); 

    //paints square objects to the screen 
    for (Bubbles aBubblesArray : BubblesArray) { 
    aBubblesArray.paint(graphics); 
    } 
} 

public void update() { 

//calls the Square class update method on the square objects 
for (Bubbles aBubblesArray : BubblesArray) aBubblesArray.update(); 
} 

    public static void main(String[] args) throws InterruptedException { 
    statusbar = new JLabel("Default"); 
    Game game = new Game(); 

    game.addMouseMotionListener(new MouseAdapter() { 
     @Override 
     public void mouseMoved(MouseEvent e) { 
      statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY())); 
      } 

     public void mouseExited(MouseEvent e){ 
     ifPaused = true; 
     } 

     public void mouseEntered(MouseEvent e){ 
     ifPaused = false; 
     } 

    }); 

    JFrame frame = new JFrame(); 

    frame.add(game); 

    frame.add(statusbar, BorderLayout.SOUTH); 
    frame.setVisible(true); 
    frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setTitle("Bubble Burst"); 
    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 

    while (true) { 
     if (!ifPaused){ 
     game.update(); 
     game.repaint(); 
     Thread.sleep(5); 
     } 
    } 
}  
} 

Bubbles Klasse:

public class Bubbles{ 

    private int circleXLocation; 
    private int circleSize; 
    private int circleYLocation = Game.WINDOW_WIDTH + circleSize; 
    private int fallSpeed = 1; 
    private int color = (int) (4 * Math.random() + 1); 
    Random rand = new Random(); 
    private int whereMouseX; 
    private int whereMouseY; 
    public int generateRandomXLocation(){ 
     return circleXLocation = (int) (Game.WINDOW_WIDTH * Math.random() - circleSize); 
    } 

    public int generateRandomCircleSize(){ 
     return circleSize = (int) (50 * Math.random() + 30); 
    } 

    public int generateRandomFallSpeed(){ 
     return fallSpeed = (int) (5 * Math.random() + 1); 
    } 

    public void paint(Graphics g){ 
     if (color == 1) g.setColor(new Color(0x87CEEB)); 
     if (color == 2) g.setColor(new Color(0x87CEFF)); 
     if (color == 3) g.setColor(new Color(0x7EC0EE)); 
     if (color == 4) g.setColor(new Color(0x6CA6CD)); 
     g.fillOval (circleXLocation, circleYLocation, circleSize, circleSize); 
    } 

    public Bubbles(){ 
     generateRandomXLocation(); 
     generateRandomCircleSize(); 
     generateRandomFallSpeed(); 
    } 

    public void update(){ 

     if (circleYLocation <= - circleSize){ 
      generateRandomXLocation(); 
      generateRandomFallSpeed(); 
      generateRandomCircleSize(); 
      circleYLocation = Game.WINDOW_HEIGHT + circleSize; 
     } 

     if (circleYLocation > - circleSize){ 
      circleYLocation -= fallSpeed; 
     } 
    } 

    public int getX(){ 
     return circleXLocation; 
    } 
    public int getY(){ 
     return circleYLocation; 
    } 

    public int getCircleSize(){ 
     return circleSize; 
    } 
} 

Antwort

-2

Schleife durch jede Blase in dem Array und Zugriff auf die öffentlichen get-Methoden Sie in der Blasenklasse erstellt haben:

Beispiel:

for (Bubble b : BubblesArray) 
{ 
    int x = b.getX(); 
    int y = b.getY(); 
} 
+0

Warum die unten Stimmen? Ist das nicht das, was das OP verlangt? –

0

Legen Sie Ihre Blasen fest, um x- und y-Werte in den Konstruktor aufzunehmen.

Public Bubble(float x, float y, int circleSize){ 
    // initialize variables here 
} 

überprüfen dann, ob sie mit der aktuellen Mausposition kollidieren, so etwas wie ...

if(e.getX() == this.getX() && e.getY() == this.getY()){ 
    // do something 
}