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

时间:2023-01-27 15:42:44

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”

2 个解决方案

#1


2  

Here is your problem:

这是你的问题:

public Droid droid_array;

Has type Droid. This is your class level property. Inside the MainGamePanel constructor you hide the class level property with this variable:

有类型Droid。这是您的类级属性。在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方法引用公共Droid droid_array类属性,该属性不是数组。

#2


0  

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; //这是Droid类型

  3. Droid[] droid_array = new Droid[5]; //this is an array of Droid type
  4. Droid [] droid_array =新的Droid [5]; //这是一个Droid类型的数组

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. This makes the Droid droid_array available there which is not an array but an object of Droid type.

但是在update()方法中,没有像droid_array这样的数组。这使得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();
  }
}

#1


2  

Here is your problem:

这是你的问题:

public Droid droid_array;

Has type Droid. This is your class level property. Inside the MainGamePanel constructor you hide the class level property with this variable:

有类型Droid。这是您的类级属性。在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方法引用公共Droid droid_array类属性,该属性不是数组。

#2


0  

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; //这是Droid类型

  3. Droid[] droid_array = new Droid[5]; //this is an array of Droid type
  4. Droid [] droid_array =新的Droid [5]; //这是一个Droid类型的数组

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. This makes the Droid droid_array available there which is not an array but an object of Droid type.

但是在update()方法中,没有像droid_array这样的数组。这使得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();
  }
}