Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving object  RSS feed

 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Could anyone help !

Have a ready made object in a square at a position of x 0 y 0
using aball.moveTo(30,30) moves the ball to x pos 30 y pos 30
but want to move the ball in steps up and then down of 10 at a time so ball is at y 40,50,60--200--90,70,60
Any help would be great thanks
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Sounds like the sort of thing where you want a Timer, so you can put a delay into the motion.
Have you looked at the threads part of the Java Tutorial yet? You will find use of the java.util.Timer there; there is another Timer class in javax.swing. which might be simpler to use for a single-threaded app.

You can set up the Timers to make the ball move at predetermined intervals.

I had to produce an app like that as an exercise. Try setting moveX and moveY parameters, then you can add those to the x and y parameters of your ball object.
To make them bounce, multiply moveX or moveY by -1 whenever your ball gets to the edge of the panel you are bouncing it on.

That any help?

CR
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sorry but it is not the thread part I am having problems with using

thread.sleep
}
catch (Exception e)
(
System.out.println(e);

its the code i have used put into inter section as was unsure if it should be there
Thanks code below (as in the inter section)

public void Bounce (int yPos)
{
this.setYpos(this.getYpos();
while (Ypos) < 199
Ypos++;
while (Ypos > 200)
Ypos-;
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.


You have got all confused in that Bounce method.
You pass a parameter yPos, but don't actually use it.
What you while loops will do is to move (one pixel at a time) from over 200 to 200, or from under 199 to 199. And your shape will stay there for ever.

I agree you have got the thread bit worked out.
Thread.sleep(50); will work. It will move your ball approx 20 times a second. Catch an InterruptedException rather than plain simple exception. I presume you have that bit inside some sort of for loop; if you useyou will get the ball moving 50x a second for 20 seconds if you set delay at 20.

I presume your Ball object already has xPos and yPos attributes set up. Also set up a changeX and changeY value, which is how many pixels your ball will move in its 20ms. Try setting random start values for the position anywhere on the panel, and random changeX or changeY values such that -5<=value<-5.

Now you will have to work out what to put in the Ball.bounce() method.

CR
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for your help will try
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did it work?
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi have not been able to work on it yet will be trying tomorrow
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!