Game code issue

So I'll start off by mentioning that I consider myself a beginner and am new to this site. Basically, I'm trying to make a game where there's falling balls, and if you click them, they disappear, but if they hit the ground, you lose. Here's the code I'm working with so far:

class Sprite {
  float x;
  float y;
  float dx;
  float dy;

  void update() {
    x += dx;
    y += dy;
  }
}

class GC extends Sprite {
  float diam;

  void render() {
    pushMatrix();
    noStroke();
    ellipse(x, y, diam, diam);
    popMatrix();

    if (y + diam / 2 > height) {
      y = height - diam / 2;
      background(255);
      fill(0);
      text("You lose.", 500, 500);
      timeIntervalFlag = 0;
    }
  }

  boolean pointInBoundingBox(float a, float b) {
    if (a > x && a < x + width &&  b > y && b < y + height)
      return true;
    else
      return false;
  }
}

GC newGC() {
  GC ball = new GC();
  ball.x = 50;
  ball.y = 50;
  ball.dx = 0;
  ball.dy = 5;
  ball.diam = 50;
  return ball;
}

int ballz = 0;
ArrayList<GC> ballList;

int lastTimeCheck;
int timeIntervalFlag = 3000;

void setup() {
  size(750, 750);
  noStroke();

  ballList = new ArrayList<GC>();

  int i = 0;
  while (i < ballz) {
    ballList.add(newGC());
    i += 1;
  }

  lastTimeCheck = millis();
}

void draw() {
  background(255);
  fill(80);
  rect(0, 725, 750, 25);

  for (GC b : ballList) {
    b.render();
    b.update();
  }

  if ( millis() > lastTimeCheck + timeIntervalFlag ) {
    lastTimeCheck = millis();
    GC b = newGC();
    b.x = random(0, 750);
    b.y = -50;
    ballList.add(b);
  }
}

void mousePressed() {
  if (GC.pointInBoundingBox(mouseX, mouseY))
    y=-1000;
}

Essentially, in order to make the falling balls "disappear" when you click on it, I tried change the ball's y coordinate when clicked so that you won't see that ball again for a very long time. However, whenever I try to run the code, I get the error "Cannot make a static reference to the non-static method pointInBoundingBox(float, float)". I've looked on Google to see what it means and how to fix it, but I'm still fairly confused about it.

Tagged:

Answers

  • The problem is in line 90. GC is te name of the class and should be the name of an object of that class

  • edited December 2015 Answer ✓

    GC.pointInBoundingBox(mouseX, mouseY)

    • When we instantiate some class w/ new, we get an unique object outta that class.
    • Inside that object goes copies from all non-staticfields from that class.
    • In the case of class GC, those fields are: x, y, dx, dy _& _diam.
    • Since each GC object has its own copy of those 5 fields above, independent from each other, we need to keep the object's reference of each 1.
    • Looking inside method pointInBoundingBox()'s implementation, we spot 2 fields there: x & y.
    • However, whose x & y is that method referring to?
    • Unless we call pointInBoundingBox() prefixed w/ the object's reference, there's no way to know about it.
    • So instead of GC, which is merely the general class name, go w/ something similar to this:
      ballList.get(0).pointInBoundingBox(mouseX, mouseY)

    • By invoking get(0) you'll get the 1st GC object that was add() to the ArrayList.

    • Therefore, fields x & y will be accessed for that particular GC object instance.
  • @GoToLoop Thank you! The code's able to run now using the ballList.get(0) method. However, nothing happens whenever I click a ball. I'm assuming that it lies in line 91. Is there anything specific I should modify that line of code into?

  • edited December 2015

    Woe, you haven't got a good understanding of OOP yet. Your question shows that.

    The problem does not so much lie in line 91 but in line 90.

    As it has been said already.

    Let me explain OOP. You derive objects from a class.

    The objects have a real positions and are what you working with.

    The class is just a abstract idea of a ball and you never really use this class except when making new balls.

    So, the class is the cookie cutter and the objects are the cookies.

    Think of a cookie cutter. A cookie cutter makes cookies, but it is not a cookie itself. The cookie cutter is the class, the cookies are the objects.

    You can't eat a cookie cutter. Only the cookies.

    Hence your line 90 is wrong.

    You're referring to the class here which doesn't really has a position of the screen.

    Instead you must check the objects (not the class).

    Where are your objects?

    Answer: They are in the ArrayList ballList. It holds your cookies. See line 63.

    So what you have to do is to loop over your objects (like you do in draw()) and check each of them with pointInBoundingBox and when one has been clicked, say y=-1000; for that object (not for the class, since it makes no sense to put the cookie maker outside the display/screen. You want to put the cookie outside the display.)

    I don't want to write that for you now.

    I want you to read what I ask, re-read your code and understand the principle. Then in mousePressed() you'll know what to do.

    Best, Chrisir ;-)

    P.S.

    Read the tutorial:

    https://www.processing.org/tutorials/objects/

    P.P.S

    using ballList.get(0) let's you check only one ball.

  • to get you started

    a)

    set ballz to 3; in line 49

    you should rename it to maximumNumberOfBalls or so

    b)

    replace your line 91 with this:

      ballList.get(0).y=-1000;
    

    it won't work but gets you started

    then do what I said above

    Send me a personal message when you need help

Sign In or Register to comment.