10 Replies - 932 Views - Last Post: 25 July 2012 - 03:42 PM Rate Topic: -----

#1 skymonkier   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Positioning and Variable Errors

Posted 14 July 2012 - 03:40 PM

I'm coding a simple 'Pong' game in Java. This is my first non-'Hello World!' project in Java. While trying to use 'javac' on my Mac in Terminal, it wouldn't compile a couple of my .java files because of some errors that I'm unable to figure out.

First off is for the computer's paddle.
public class ComputerPaddle
{
	// Declare needed variables.
	private int yPos = 0, score;
	final int XPOS = 460;
	
	// This takes the y position of the ball and applies it to
	// the y position of the ComputerPaddle.
	public ComputerPaddle(int ballPos)
	{
		//Sets the y position and sets the score to 0.
		setPos(BallPos);
		setScore(0);
	}
	
	public void setPos(int pos)
	{
		// Same setup as the HumanPaddle.
		this.yPos = pos;
		
		// If the y position from the upper left-hand corner is
		// 70 pixels from the bottom of the applet,
		if(yPos > 230)
		{
			// Set it back to 70 pixels away.
			setPos(230);
		}
		
		// If the y position from the upper left-hand corner is
		// less than zero (outside the applet),
		else if(yPos < 0)
		{
			// Set the y position to 0.
			setPos(0);
		}
	}
	
	public int getPos()
	{
		>>return yPos();<<
	}

I got two errors for this part of it. The error was that it couldn't find the value for the method marked with the >><<. The second error is that it depends on Ball.java, which I'll get to in a second. (I hate how horribly vague Terminal is.)

Now, the Ball.java came up with an 'unexpected type' on the area marked with the >><<.
public class Ball
{
	// Delcare the variables we need.
	// The variables for the x and y positions...
	private int xPos,yPos;
	// The variables for the x and y direction of the ball.
	// 'dy' is -5 because the applet window has an invisible
	// 'grid'. This grid has it's origin at (0, 0), which is in
	// the top left-hand corner of the applet.
	public int dx = 5, dy = -5;
	
	public Ball()
	{
		// Set the initial ball position to around the center
		// of the applet screen.
		setPos(250, 140);
	}
	
	public void setPos(int x, int y)
	{
		// Set the position of the ball.
		this.xPos = x;
		this.yPos = y;
	}
	
	public int getX()
	{
		// Get the x position of the ball.
		return xPos;
	}
	
	public int getY()
	{
		// Get the y position of the ball.
		return yPos;
	}
	
	// Now we create the method used to move the ball.
	public void move()
	{
		// It takes the x and y position and then adds their
		// current direction of movement to them, giving us
		// a shift in the ball's position on the applet grid.
		>> setPos(this.getX() = dx, this.getY() + dy); <<
	}

So, can anyone help me with these small problems?

Cheers!
skymonkier

Is This A Good Question/Topic? 0
  • +

Replies To: Positioning and Variable Errors

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Positioning and Variable Errors

Posted 14 July 2012 - 06:23 PM

For the first error, compare line 40 in the first listing with line 35 of the second. I think you'll see your error.

For the second error, I think you have an equals sign, '=', where you meant to have an addition sign, '+'.

Welcome to DIC. Good first post!

This post has been edited by GregBrannon: 14 July 2012 - 06:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 skymonkier   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Positioning and Variable Errors

Posted 15 July 2012 - 06:26 AM

Thanks! Ball.java compiled perfectly. +1 rep for you! :flowers:

Now, there's only one last problem. This also has to do with the ComputerPaddle. I fixed the simple syntax error I made, but apparently it needs the Ball's position (BallPos, in the Ball.class file), and I had thought that since the Ball.class was in the same folder I was compiling to, it would work fine.

This isn't the case sadly. How do I get it to compile? I've looked around the internet some and read of something about 'creating an empty class,' how would I go about doing this?

Cheers!
skymonkier
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Positioning and Variable Errors

Posted 15 July 2012 - 06:43 AM

Creating an empty class? Where did you come up with that? Don't bother explaining.

Instead, consider this:

The ComputerPaddle() constructor is supposed to receive the y component of the ball position as a parameter. If the ComputerPaddle() constructor is used properly, the ComputerPaddle instance will know the ball's y coordinate when it is created.

From the point the computer paddle instance is created, the Ball class' public getters (or accessors), getX() and getY(), can be used to return the x and y coordinates of a Ball instance. So if you have an instance of Ball in your driver or test class (the class with a main() method, perhaps):

Ball ball = new Ball();

you can then get the position of the ball object:

int ballX = ball.getX();
int ballY = ball.getY();
Was This Post Helpful? 0
  • +
  • -

#5 skymonkier   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Positioning and Variable Errors

Posted 15 July 2012 - 07:17 AM

Hmm. I tried your method in the way I thought it would work, but it only brought on more errors. I think this is on my side for it not working, so I'm asking you to help me figure out how to do that. :P

The errors I'm getting are 'cannot find symbol'.
This is the current way I have it done:
public class ComputerPaddle
public class ComputerPaddle
{
	// Declare needed variables.
	private int yPos = 0, score;
	final int XPOS = 460;
	// Create an instance of the Ball to get it's position.
	>> Ball ball; <<
	
	// This takes the y position of the ball and applies it to
	// the y position of the ComputerPaddle.
	>> public ComputerPaddle(int ballPos)
	{
		Ball ball = new Ball();
		int BallPos = ball.getY;
		//Sets the y position and sets the score to 0.
		setPos(BallPos);
		setScore(0);
	} <<
	
	public void setPos(int pos)
	{
		// Same setup as the HumanPaddle.
		this.yPos = pos;
		
		// If the y position from the upper left-hand corner is
		// 70 pixels from the bottom of the applet,
		if(yPos > 230)
		{
			// Set it back to 70 pixels away.
			setPos(230);
		}
		
		// If the y position from the upper left-hand corner is
		// less than zero (outside the applet),
		else if(yPos < 0)
		{
			// Set the y position to 0.
			setPos(0);
		}
	}
	
	public int getPos()
	{
		return yPos;
	}
	
	// These two blocks get and set the int score.
	public void setScore(int score)
	{
		this.score = score;
	}
	
	public int getScore()
	{
		return this.score;
	}
}


Oh, and I forgot to mention that the errors always point to the 'Ball' references.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Positioning and Variable Errors

Posted 15 July 2012 - 07:21 AM

Are the >> and << symbols in your actual code or did DIC add them? They shouldn't be there
Was This Post Helpful? 0
  • +
  • -

#7 skymonkier   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Positioning and Variable Errors

Posted 15 July 2012 - 07:29 AM

View PostCasiOo, on 15 July 2012 - 07:21 AM, said:

Are the >> and << symbols in your actual code or did DIC add them? They shouldn't be there


No, I add those to point out where my problems are. I'm not stupid enough to put >><<'s in my code. :P
Was This Post Helpful? 0
  • +
  • -

#8 skymonkier   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Positioning and Variable Errors

Posted 17 July 2012 - 07:17 PM

Sorry about not following up with this thread and letting it die, but I was a bit busy for a couple days. Can anyone help me understand how to call an instance of Ball() in my file so it can compile? It doesn't work, and there's more info if you read above.

Thanks and cheers,
skymonkier
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Positioning and Variable Errors

Posted 18 July 2012 - 08:58 AM

I wrote a Pong game once a while back. Here are a few pointers from my experiences:
  • Don't design ComputerPaddle and PlayerPaddle classes. You only need one Paddle class. Let the main Game class handle moving the ComputerPaddle in relation to the Ball. This way, the Paddle classes don't need to store references to the Ball. Just store a single Ball in your Game class.

  • Check out the java.awt.geom library for classes like Rectangle2D and Ellipse2D, which will make it easier to draw your Paddles and Ball.

Was This Post Helpful? 0
  • +
  • -

#10 skymonkier   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Positioning and Variable Errors

Posted 25 July 2012 - 03:00 PM

View Postmacosxnerd101, on 18 July 2012 - 08:58 AM, said:

I wrote a Pong game once a while back. Here are a few pointers from my experiences:
  • Don't design ComputerPaddle and PlayerPaddle classes. You only need one Paddle class. Let the main Game class handle moving the ComputerPaddle in relation to the Ball. This way, the Paddle classes don't need to store references to the Ball. Just store a single Ball in your Game class.

  • Check out the java.awt.geom library for classes like Rectangle2D and Ellipse2D, which will make it easier to draw your Paddles and Ball.


Ummm, I think I understand the basics of how this would work, but how do I put the Ball in the Game class and have the Game control the ComputerPaddle separately?

I'm so newbish. Can you demonstrate, please?
Was This Post Helpful? 0
  • +
  • -

#11 Sheph   User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: Positioning and Variable Errors

Posted 25 July 2012 - 03:42 PM

Mac may be right about the design, but you should take the time to learn how you can solve the problem your having with the Ball reference. If you want to access a reference to an object, you have to understand where that object will be initialized, and how you can get that reference to your class.

Here, we will initialize it in a Game class maybe:

public class Game {
    Ball ball; // a field of a Game object

    public Game() {
        ball = new Ball(); // notice I don't give ball a type
        // Ball ball = new Ball(); wouldn't instantiate our field "ball"
        ball.setPos(0,0);
    }
}


Then you have to think about where ComputerPaddle will be initialized. If it is in Game, then you're almost done

public Game() {
    ball = new Ball();

    compPaddle = new ComputerPaddle(ball);
}

then, change ComputerPaddle constructor to accept a Ball.

public class ComputerPaddle {
    Ball ball;

    public ComputerPaddle(Ball B)/> {
        this.ball = b;
    }
}


Hopefully you will learn how to solve this problem mechanically. After that, you can worry about improving your design, as mac said.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1