File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Making Y-- Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making Y--" Watch "Making Y--" New topic
Author

Making Y--

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
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?





Live And Let Learn.
Jaap Vermeer
Greenhorn

Joined: Apr 04, 2006
Posts: 16
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

Joined: Mar 26, 2006
Posts: 124
I need another way to do this... something garenteed to work.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
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

Joined: Mar 26, 2006
Posts: 124
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

Joined: Feb 18, 2005
Posts: 1013
    
    3
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19072
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Mar 26, 2006
Posts: 124
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

    Joined: Mar 26, 2006
    Posts: 124
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Making Y--