Add {}s after the if and put the println inside the {}s
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Add {}s after the if and put the println inside the {}s
If you don't understand my answer, don't ignore it, ask a question.
if ( x >= W-B-HALF_BALL_SIZE ) x_inc = -1*S {System.out.println (x_inc)}; ?
Did that and I just see 1 when its moving to the right, and -1 when its moving to the left.
You left the assignment statement outside of the {}s
if (..) { x_inc = -1*S ; System.out.println ("x_inc=" +x_inc); }
Do you know what makes the value change and how that changes the direction of movement?
Last edited by Norm; May 13th, 2012 at 11:43 AM.
If you don't understand my answer, don't ignore it, ask a question.
Ermm
Is it, if x is greater than the width - border offset - hall ball size it changes direction? and gives -1 values to x until it reaches the other side
So if x > 790 it reverses and gives x values -1?
Describe the place where x's value is reversed using terms of the shapes, not numeric values,
Do NOT code 790. Use the values of variables. I'm testing with a window that is 300x400 defined using the W and H variables.
What variable? x is the location of the ball.gives x values -1?
If you don't understand my answer, don't ignore it, ask a question.
x >= W - Halfballsize
I know x_inc gives it a -1, but I don't understand how it does that.
x_inc gives what a -1?
Can you explain what variable? How does x_inc give the other variable a -1?
Think about how the position of the ball changes. What should happen when the ball gets to a side?
Just work with x values for now. How does the x value change as the ball moves to the right? What will be different when the ball moves to the left?
If you don't understand my answer, don't ignore it, ask a question.
When it goes to the right it gets 1 which adds to the x value, while going to the left gives it a -1
It's not really 1, its the value of the variable: S. Change S's value and you'll see.
When does the sign of S change from + to - and from - to +?
If you don't understand my answer, don't ignore it, ask a question.
Oh is that what S is for....... Yeah I changed S to 5 and when it hit a wall it sped up.
The S changes depending on the condition that is met I'm guessing.
As it is -1 or 1 * S.
The code is poorly written when it comes to meaningful variable names.
When does the sign of x_inc change?
If you don't understand my answer, don't ignore it, ask a question.
if ( x >= W-B-HALF_BALL_SIZE ) -1
if ( x <= 0+B+HALF_BALL_SIZE ) 1
When either of these conditions are met.
Yeah I was given it and had to make a pong game, the only parts I added were the paddles.
Can you explain in English to show that you understand what the code is doing.
If you don't understand my answer, don't ignore it, ask a question.
if ( x >= W-B-HALF_BALL_SIZE ) x_inc = -1*S;
if x is greater or equal to the width - border offset - halfballsize, it would then call forth x_inc and multiply S by -1. Therefore creating a making the x_inc = -1
What visual thing do all those numbers represent?
Now think in terms of the paddles. When would you want to change the sign of x_inc if the ball is moving to the left towards paddle1?
If you don't understand my answer, don't ignore it, ask a question.
When it touches the right side of paddle 1.
I'm not sure what varibles to put in there, would I be using p1X ?
Go to the paper and pencil diagram I asked you to draw. Label it with the variables that define the locations of the paddle and the ball. Then look at that drawing to get the variable names you need in the code.
If you don't understand my answer, don't ignore it, ask a question.
X variables for paddle is p1X , Y is p1Y.
Ball position is x and y?
Also what does this part mean?
x += x_inc;
y += y_inc;
Thanks for the help btw. Might as well learn how to understand it, but short on time, so hope it gets done soon.
What does a change to the value of x do?
If you don't understand my answer, don't ignore it, ask a question.
Hmm? Don't understand that question.
What is the variable: x used for in the program? What happens visually when the value of x is changed?
If you don't understand my answer, don't ignore it, ask a question.
The ball position
Does that answer your question asked in post#43?
If you don't understand my answer, don't ignore it, ask a question.
Was wondering what the += means.
Also how would I get started on collision detecting the paddle and the ball?
Is it similar to the layout of the wall collision?
Yes, look at the diagram you've drawn on paper and get the variable names from that.Is it similar to the layout of the wall collision
var += val is shorthand notation for var = var + val.
If you don't understand my answer, don't ignore it, ask a question.