简体   繁体   English

Java为什么认为我的对象是变量?

[英]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). 我在Sprite类中编写了一个collidesWith(Sprite s)方法,该方法检查球是否与桨碰撞(这是可行的,而不是问题)。 I have two objects extending my sprite class, a ball and a paddle object. 我有两个扩展我的Sprite类的对象,一个球和一个球拍对象。 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" 我也尝试过ball.collidesWith(paddle)和其他组合,但是它总是对桨(和当我使用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. 该程序知道boxOne和boxTwo是我的Sprite类的子类。 Anyone know why they wouldn't work the same? 有人知道为什么他们不一样吗?

class Paddle {}

Paddle is a class. Paddle是一类。

Paddle paddle;

paddle is a variable which doesn't yet refer to an instance of the Paddle class (aka an object). paddle是一个变量,尚未引用Paddle类的实例(也称为对象)。

Paddle paddle = new Paddle();

This paddle is a variable and refers to an instance of Paddle . paddle是一个变量,它引用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 . 是一个表达式,该表达式在名为ball的变量所引用的对象上调用名为collidesWith的方法,并将由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. 如果尚未在此表达式的相同或封闭的词法范围内定义名为ball的变量和名为paddle的变量,则该表达式无效。 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. 如果您已创建变量ballpaddle但尚未将它们设置为引用某个实例,则该表达式将编译但无法正确执行。 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: 或者,如您所指出的,如果您属于Ball类,那么您可能会遇到以下情况:

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. 在这种情况下, this是一个不必定义的变量,它引用了在其上调用方法的对象。 Wherever you write the expression, paddle has to be a variable that's declared somewhere visible. 无论您在哪里编写表达式, paddle都必须是一个在可见位置声明的变量。

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. 我的猜测(与您所提供的有限信息一样,我只能做些猜测):您没有在球类知道的范围内声明paddle 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. 您的管理类将对两个桨中的每一个都有一个ball.collidesWith(paddle)检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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