Jump to content

  • Log In with Google      Sign In   
  • Create Account


[JAVA]First Game help[JAVA]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 20 February 2013 - 08:25 PM

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.



Sponsor:

#2 David.M   Members   -  Reputation: 731

Like
6Likes
Like

Posted 20 February 2013 - 09:02 PM

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.



#3 Nymall   Members   -  Reputation: 192

Like
0Likes
Like

Posted 22 February 2013 - 12:23 PM

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, 22 February 2013 - 12:25 PM.

My favorite error yet:

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS