Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

YPOS

 
Mark Graham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you tell me please after this code has been entered how to move the yPos down by by 10 pixels

public void moveTo (int xPos, int yPos)

this.setXpos(xPos);
this.setYpos(yPos);

using aball.moveTo(100,100) // code works fine

But unsure how to just move the yPos

Thanks in advance
 
Jody Brown
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does aball.moveTo(aBall.getXpos(), aBall.getYpos() - 10) work?

Edit: Or +10, depending on which is up and which is down.
[ June 02, 2006: Message edited by: Jody Brown ]
 
Mark Graham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void singleBounce()
{
this.moveTo aBall.getXpos();
this.getYpos() - 10);

Does not work any ideas ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void singleBounce()
{
this.moveTo aBall.getXpos();
this.getYpos() - 10);

Does not work any ideas ?
No. it won't work.

Are you on the same course as Richard Jones?

What you need to do is to set a position variable (you might be better setting this is a Ball class rather than in your display). Then you set up something that "ticks" probably 50 times a second. At 50 times a second you can't see the "ticks;" at 20 times a second or less, it will start to appear jerky. There are at least three ways to do this:-
  • use the sleep() method of Thread.
  • Use the Timer class in javax.swing.Timer
  • use the Timer class in java.util.Timer
  • Look in the Java Tutorial. In fact you ought to have the Java tutorial and the API specification (this is 1.5.0) visible on screen whenever you program anything.

    Then for each "tick" you move the position.

    Try to set the value of change so you don't move more than about 250 pixels a second. Try that and get it to work. Your ball will drift off the edge of the screen and vanish, but there is a simple way to get it to bounce when it hits the edge of the screen.

    Also look at what Richard Jones has posted on this forum here and about random numbers here, and see whether any of that discussion is of any use to you.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic