Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on game development in android

I have a class called droid in my model package and i have a constructor called update() in my main game panel class

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"

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

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"

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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[] droid_array

Once you leave the MainGamePanel constructor the Droid[] droid_array variable goes out of scope.

The Update method references the public Droid droid_array class property, which is not an array.

share|improve this answer
    
Yes this was exactly it. Thanks...now if only someone can help me with my other question regarding view vs surface view :) –  heckubiss Sep 10 '13 at 1:01

Basically, there are two variables with the name 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.

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.

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();
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.