Home  >  Forum  >  Core Java
Post New Query

Why does Java think my object is a variable?

joined on
March 15,2013
Asked on October 18,2013

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){

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?

joined on
March 14,2013
Replied on October 18,2013

What I understand that, you have a super class  Sprite and you are extending it to create two class Ball and Paddle.  You need to check if you are creating any object named as paddle inside Ball class, if yes, is this in scope of that method where paddle object is being is used?

Write Answer


Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us