• Advertisement
Sign in to follow this  

[JAVA]First Game help[JAVA]

This topic is 1889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So for this game I want to make the ball to change direction when it hits the bottom of the screen. So i have 

 

public void ballmove()
{
    y += 1;
}

 

that moves it no problem but how would i make it go the opposite direction when it hits the top of the screen or bottom.

Share this post


Link to post
Share on other sites
Advertisement

Not a answer, as the above poster did it pretty well, just a caveat that's got me a couple of times(To hopefully make things easier on you).

 

When you're checking the size of the screen, remember to subtract the height from the graphic from the size of the screen. If you're tracking from the top of the graphic, it'll mean that the entire ball will disappear beyond the bound of the screen before reversing. I'd also suggest using a Boolean value to control the up down, so you can do something like this:

 

public void ballmove()
{
  if(y >= screenheight){
    ismoveup = false;
  }
  if(y <= 0 + ballheight){
    ismoveup = true;
  }
  if(ismoveup == true){
    y += 1;
  } else {
    y -= 1;
  }
}
//This is assuming that your origin(0y) is the bottom of the screen
//you'll have to set the height of the ball in ballheight and the height of the screen in screenheight somewhere else

 

This isn't the most elegant solution, but should help you move forward.

Edited by Nymall

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement