简体   繁体   English

如何访问java中对象数组中对象的变量

[英]how to access variables in objects in an array of objects in java

I am working on game development in android 我正在研究android中的游戏开发

I have a class called droid in my model package and i have a constructor called update() in my main game panel class 我的模型包里有一个名为droid的类,我的主游戏面板类中有一个名为update()的构造函数

I would like to make an array of droids and access them in my main game panel and from constructors within the main game panel class. 我想制作一系列机器人并在我的主游戏面板和主游戏面板类中的构造函数中访问它们。 I am able to do this from the main game panel constructor but not from the update constructor. 我可以从主游戏面板构造函数执行此操作,但不能从更新构造函数执行此操作。 ie Whenever I try to access the x position of one of the droids in the update constructor of the main game panel class I get this error: "The type of the expression must be an array type but it resolved to Droid" 即每当我尝试访问主游戏面板类的更新构造函数中的某个机器人的x位置时,我会收到此错误:“表达式的类型必须是数组类型,但它已解析为Droid”

package net.test.droid.model;

public class Droid {

private Bitmap bitmap;  // the actual bitmap
private int x;          // the X coordinate
private int y;  
private boolean touched;    // if droid is touched/picked up

public Droid(Bitmap bitmap, int x, int y) {
    this.bitmap = bitmap;
    this.x = x;
    this.y = y;
}



public Bitmap getBitmap() {
    return bitmap;
}
public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}



public void draw(Canvas canvas) {
        canvas.drawBitmap(bitmap, x, y, null);
}
}

in main game 在主要游戏中

public class MainGamePanel extends SurfaceView implements
    SurfaceHolder.Callback {

  public Droid droid_array;
  public MainGamePanel(Context context) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);
    Droid[] droid_array = new Droid[5];
    for (int i = 0; i < 5; i++) {
        droid_array[i] = new Droid(BitmapFactory.decodeResource(
                getResources(), R.drawable.ic_launcher),                              droid_x_pos + i*10, droid_y_pos);
    }
droid_array[1].setX(666);
}

the last line works fine however whenI try to use it in update() I get the error 最后一行工作正常但是当我尝试在update()中使用它时,我得到了错误

public void update() {
test=droid_array[1].getX();
}

the above line returns error "The type of the expression must be an array type but it resolved to Droid" 上面的行返回错误“表达式的类型必须是数组类型,但它解析为Droid”

Here is your problem: 这是你的问题:

public Droid droid_array;

Has type Droid . 有类型Droid This is your class level property. 这是您的类级属性。 Inside the MainGamePanel constructor you hide the class level property with this variable: MainGamePanel构造函数中,您使用此变量隐藏类级属性:

Droid[] droid_array

Once you leave the MainGamePanel constructor the Droid[] droid_array variable goes out of scope. 离开MainGamePanel构造函数后, Droid[] droid_array变量超出范围。

The Update method references the public Droid droid_array class property, which is not an array. Update方法引用public Droid droid_array类属性,该属性不是数组。

Basically, there are two variables with the name droid_array . 基本上,有两个名为droid_array变量。

  1. public Droid droid_array; //this is of type Droid
  2. Droid[] droid_array = new Droid[5]; //this is an array of Droid type

At the last line of the MainGamePanel(Context contect) constructor, droid_array is accessible as an array because droid_array is of the method scope. MainGamePanel(Context contect)构造函数的最后一行, droid_array可以作为数组访问,因为droid_array是方法范围。

But in the update() method, there is no such array as droid_array . 但是在update()方法中,没有像droid_array这样的数组。 This makes the Droid droid_array available there which is not an array but an object of Droid type. 这使得Droid droid_array在那里可用,它不是数组而是Droid类型的对象。

You will need to do something like this. 你需要做这样的事情。

public class MainGamePanel extends SurfaceView implements
    SurfaceHolder.Callback {

  public Droid[] droid_array;
  public MainGamePanel(Context context) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    droid_array = new Droid[5];
    for (int i = 0; i < 5; i++) {
        droid_array[i] = new Droid(BitmapFactory.decodeResource(
                getResources(), R.drawable.ic_launcher),                               droid_x_pos + i*10, droid_y_pos);
    }
    droid_array[1].setX(666);
  }

  public void update() {
      test=droid_array[1].getX();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM