如何为数组中的对象返回实例变量

时间:2016-05-19 23:58:42

标签: java arrays oop instance-variables

我是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 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:-2)

遍历数组中的每个Bubble并访问您在Bubble类中创建的公共get方法:

示例

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