【问题标题】:how to access variables in objects in an array of objects in java如何在java中访问对象数组中的对象中的变量
【发布时间】:2013-09-09 17:33:44
【问题描述】:

我正在做 android 游戏开发

我的模型包中有一个名为 droid 的类 我的主游戏面板类中有一个名为 update() 的构造函数

我想制作一个机器人数组,并在我的主游戏面板和主游戏面板类中的构造函数中访问它们。 我可以从主游戏面板构造函数执行此操作,但不能从更新构造函数执行此操作。即,每当我尝试在主游戏面板类的更新构造函数中访问其中一个机器人的 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);
}
}

在主游戏中

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

最后一行工作正常,但是当我尝试在 update() 中使用它时,我得到了错误

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

以上行返回错误“表达式的类型必须是数组类型,但它解析为 Droid”

【问题讨论】:

    标签: java android arrays object


    【解决方案1】:

    这是你的问题:

    public Droid droid_array;
    

    类型为Droid。这是您的班级级别的财产。在 MainGamePanel 构造函数中,您可以使用此变量隐藏类级别属性:

    Droid[] droid_array
    

    一旦您离开 MainGamePanel 构造函数,Droid[] droid_array 变量就会超出范围。

    Update 方法引用了public Droid droid_array 类属性,它不是一个数组。

    【讨论】:

    • 是的,就是这样。谢谢......现在如果只有有人可以帮助我解决关于视图与表面视图的其他问题:)
    【解决方案2】:

    基本上,有两个变量名为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

    MainGamePanel(Context contect) 构造函数的最后一行,droid_array 可作为数组访问,因为droid_array 属于方法范围。

    但是在update()方法中,没有droid_array这样的数组。这使得Droid droid_array 在那里可用,它不是数组而是Droid 类型的对象。

    你需要做这样的事情。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多