2

I'm making a game where basically you dodge incoming asteroids as a spaceship (it's for a first year university project).

I've got a problem though, I call in 6 object from a class (asteroids), they are called in via an array. I need to be able to use their x and y positions outside of the class in order to detect if the user controlled sprite has collided with one.

Whenever I try to refer to the x or y position of the asteroids outside of the class I get the error "Cannot make a static reference to the non-static field obstacle.posx".

Below is an MCVE of my code. It calls in the objects and they move down the screen. I have removed the user controlled sprite and images for now as all I need to know is how to refer to the objects positions outside of the class.

*I've added some changes that display what I'm trying to achieve

obstacle [] asteroid; 
int x;
int y = 400;
int velocity = 10;

void setup ()
{
  size (700, 500);

  asteroid = new obstacle[6];
  for (int i = 0; i < asteroid.length; i++) {
    asteroid[i] = new obstacle();
  }
}

void draw () {
  background(0);
  collision();
  rect (x,y,30,30);

  for (int i = 0; i < asteroid.length; i++) {
    asteroid[i].display();
    asteroid[i].move();
  }
}

void keyPressed () {
  if (key == CODED) {

    if (keyCode == RIGHT) {  
      x += velocity;
    }

    if (keyCode == LEFT) {
      x -= velocity;
    }
  }
}

void collision () {
  if (x == obstacle.posx && y == obstacle.posy) {
    println("Hit");
  }
}


class obstacle {


  int velocity = 6;
  int posx;
  int posy = height;



  void display () {
    rect (posx, posy, 50, 50);
  }

  void move() {
    posy += velocity;
    if (posy >= height) {
      posy = (int(random(-500, -50))); 
      posx = (int(random(20, 650))); 
    }
  }
}
1
  • your collision() method is still problematic. Try something like this: void collision () { for (int i = 0; i < asterioid.length; ++i) { if (x == asteroid[i].posx && y == asteroid[i].posy) { println("Hit"); break; } } } Commented Mar 24, 2015 at 3:45

2 Answers 2

2

Re: your last comment, that's your problem. You can't refer to obstacle.posx because obstacle is the class type, and posx isn't static. You could move that inside your for loop and do System.out.println(asteroids[i].posx) (assuming posx is visible there (i.e. it's public).

Sign up to request clarification or add additional context in comments.

1 Comment

I've made some changes that more accurately show what I'm trying. I'm trying to use collision detection by checking if one of the objects is in the same position as the user. If it is, some code will run that displays a gameover screen. But I can't seem to refer to the position of the object.
0

You might be confusing obstacle class with instances of this class. You shouldn't try to access the members of instance through the class (via obstacle.posx), but through instances.

The same way you call methods/functions like move(),display() you should be able to access posx.

Here's a slightly modified version of draw():

void draw () {
  background(0);
  println(asteroid[0].posy);
  for (int i = 0; i < asteroid.length; i++) {
    asteroid[i].display();
    asteroid[i].move();
  }
}

Notice the values changing in the console. You're not accessing posy directly via the class, you accessing the first obstacle instance (stored at index 0 in the asteroid array) and using dot notation (same way you move/display the others). Hope this is clear. You were so close.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.