简体   繁体   中英

Why does Java think my object is a variable?

Ok so I'm trying to make a simple pong game. I have a paddle that follows the mouse and a ball that bounces around. I wrote a method collidesWith(Sprite s) inside of my Sprite class that checks if the ball collides with the paddle (this works and isn't the problem). I have two objects extending my sprite class, a ball and a paddle object. So inside of my ball class I'm trying to check if it collides with the paddle. So I've tried

if(this.collidesWith(paddle) == true){
    System.out.println("They touched");
}

I've also tried ball.collidesWith(paddle) and other combinations but it always says the same thing about the paddle (and the ball when I use ball.collidesWith ) "Cannot find symbol. Symbol: variable paddle(or ball). Location: class Ball"

So if I'm reading this right, it thinks that the paddle (and ball) are variables and it's complaining because it can't find them. How can I make it understand I am passing in objects, not variables?

For extra information, an earlier assignment had me make two boxes and for them to change colors when they were colliding. In that assignment I used very similar code to above with

if(boxOne.collidesWith(boxTwo) == true){
      System.out.println("yes");
}

And in this code it worked just fine. The program knew that boxOne and boxTwo were child classes of my Sprite class. Anyone know why they wouldn't work the same?

class Paddle {}

Paddle is a class.

Paddle paddle;

paddle is a variable which doesn't yet refer to an instance of the Paddle class (aka an object).

Paddle paddle = new Paddle();

This paddle is a variable and refers to an instance of Paddle .

ball.collidesWith(paddle)

is an expression that invokes the method named collidesWith on the object referred to by the variable named ball and passes it the object referred to by the variable named paddle . If you haven't defined a variable named ball and a variable named paddle in the same or an enclosing lexical scope of this expression, then the expression isn't valid. If you've created variables ball and paddle but haven't set them to refer to some instance, then the expression will compile but won't execute correctly. You should have something like this:

Ball ball = new Ball();
Paddle paddle = new Paddle();
if (ball.collidesWith(paddle)) { ... }

Or if, as you indicated, you're inside the Ball class, you may have something like:

class Ball {
    boolean collidesWith(Paddle paddle) {
        ...
    }

    void somethingElseWithAPaddle(Paddle paddle) {
        if (this.collidesWith(paddle)) { ... }
    }
}

In that case, this is a variable that you don't have to define and refers to the object on which the method was invoked. Wherever you write the expression, paddle has to be a variable that's declared somewhere visible.

My guess (as with the limited information you're giving I can't do much more than guess): you did not declare the paddle in a scope that the ball class knows. Or maybe you did declare it, but it is not defined at the time this code runs.

Normally, you'll write a managing class that knows of and maintains the paddle instances and at the same time knows the ball instance. Instead of having the ball check for collisions, you'd make your managing class do this instead. Considering the managing class would also be responsible for building your paddles and ball, it would already have them in the right scope. Your managing class would have a ball.collidesWith(paddle) check for each of the two paddles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM