Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making Y--

 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem making the ball decrease after it hits the border "m". Y is ++ at first but when it hits "m" I have it so Y is -- but it basically cancels the ++ Y therefore the ball does not increase. How can I get the ball to bounce back off the wall?



 
Jaap Vermeer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make a new int yIncrement and make it -1 if you want to go down and +1 if you want to go up and then do
y += yIncrement;
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need another way to do this... something garenteed to work.
 
Ryan McGuire
Ranch Hand
Posts: 1073
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin vanPutten:
I need another way to do this... something garenteed to work.


What's wrong with what Japp suggested? The only part he left unwritten for you to work out on your own is when to change the value of yIncrement.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it doesn't work. After reading what you wrote, I tried it, and it didn't work. First of all
make it -1 if you want to go down and +1 if you want to go up
doesn't make sense because using the y-axis in an applet it should be +1 for down and vice versa. I'm sorry for what i said about having something garenteed to work but this peice of code is getting to me. All I want is for the ball to bounce off walls. But theres something I'm missing... that I just can't think of. if Japps way does work, can somebody explain more?
 
Ryan McGuire
Ranch Hand
Posts: 1073
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so setting yIncrement to +1 will make the ball go down and setting it to -1 will make it go up. When would you want to change from +1 to -1 and when would you want to change from -1 to +1?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what he is saying is that instead of doing y++ and y--, do a

y += yDirection;

if you want the ball to move one direction, you make yDirection = 1. if you want it to move in the other direction, you make it yDirection = -1.

the trick is to figure out when it should be which value, and when to set it accordingly.
 
Henry Wong
author
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

doesn't make sense because using the y-axis in an applet it should be +1 for down and vice versa


So... because of a wording issue, you are going to dismiss the entire concept?


All I want is for the ball to bounce off walls. But theres something I'm missing... that I just can't think of. if Japps way does work, can somebody explain more?


In pseudo code, this is what Jaap meant...



Henry
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When your ball goes down it increases its Y by one right? Then logically if it should go up which is the opposite direction you would decrease Y by one.


[ April 29, 2006: Message edited by: Unnar Bj�rnsson ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do it, which I prefer, is to multiply the ballDirection variable by -1 whenever the ball is supposed to bounce.

CR
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help... all of you. I got it to bounce. I didn't mean to be an ass about it, and I apologize if I have offended anybody. I changed the code a bit. Now I'm at the part where I have to move the computers paddle. You can see I have it set to yy = Y which means when the ball moves one way, the paddle moves that way as well. But it only works on the balls first start movement and it moves until it hits the first wall. Then, the paddle sits still. How can I get it to move up with the Ball?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried out your very first coding. The real reason it doesn't bounce is that in your BallTimerListener, you have y++ and y-- both being done together.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woodstock? Is that in Oxfordshire? I have some friends in that part of the Country, at Glympton.

You still haven't got it to work properly. I don't have time to go through the whole thing line by line, and I am not quite sure how you intend it to bounce.
  • Can you get away with fewer listeners and Timers?
  • You might be able to pass an increment (eg 1, -1) to your Listener classes by giving them constructors.
  • Did you really mean to write if(Y==Y) ?

  • CR
     
    Martin vanPutten
    Ranch Hand
    Posts: 124
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    CR - its Woodstock, New brunswick. And yes I did mean to... then I looked at it after you mentioned it, and... well... I don't know... what.... i was.... thinking....
    [ April 29, 2006: Message edited by: Martin vanPutten ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic