aspose file tools*
The moose likes Beginning Java and the fly likes YPOS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "YPOS" Watch "YPOS" New topic
Author

YPOS

Mark Graham
Greenhorn

Joined: Jun 02, 2006
Posts: 2
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

Joined: Nov 09, 2005
Posts: 43
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

Joined: Jun 02, 2006
Posts: 2
public void singleBounce()
{
this.moveTo aBall.getXpos();
this.getYpos() - 10);

Does not work any ideas ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: YPOS