[JAVA]First Game help[JAVA]

Started by
1 comment, last by Nymall 11 years, 2 months ago

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.

Advertisement

I would change y += 1 to y += ballVelocity. ballVelocity would then initially be 1. If the ball's y position is > the screen's height or is < 0, reverse ballVelocity.

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.

My favorite error yet:

Parse error: Syntax error, unexpected '$pants'...

This topic is closed to new replies.

Advertisement