2016-05-19 42 views
-1

我是java中图形学的新手,我目前正在研究一款游戏。本质上,气泡上升,用户必须通过将鼠标移到它们上面来弹出它们。如何返回数组中对象的实例变量

我已经做了气泡,但现在碰撞检测,我需要能够找到x和坐标以及圆的直径。我怎样才能返回每个气泡的实例变量(来自Bubbles对象数组)。你可以在下面看到我的代码。如果您发现编码错误,请告诉我。

游戏类:

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); 
     } 
    } 
}  
} 

泡沫类:

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; 
    } 
} 

回答

-2

遍历数组中的每个气泡,并访问您的气泡类别创建的公共get方法:

示例

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

为什么向下票?这不是OP要求的吗? –

0

将气泡设置为在其构造函数中包含x和y值。

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

然后检查,看看他们是否与你当前鼠标位置发生碰撞,像...

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