Sign in to follow this  
xDancinpoptartx

[JAVA]First Game help[JAVA]

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this