Jump to content
  • Advertisement
Sign in to follow this  
xDancinpoptartx

[2D Java Help] Need help with pong remake.[2D Java Help]

This topic is 2094 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

I need help makeing the ball hit the bottom and bouncing back up. I tried this but it just hits and stays in one spot which i know why but what else would i do?

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

 

 

Share this post


Link to post
Share on other sites
Advertisement

Well, it just adds 1 to y so removing 1 if y == 420 will just put it back at 420.

You need to have some kind of variable indicating which way the ball is moving. I'd suggest you add a vector and they adding it to the position and changing the vector to a negative y-value whenever you hit the wall and vice versa for the other wall.

Share this post


Link to post
Share on other sites

That's really little information you provided us there. So yeah, I agree with Navall, you have to start decreasing your value to make the ball come the opposite way, so  direction information would be very good not only for that, but also to make some calculation based on the angle the ball hit the border and to determine the angle at which it'll keep on moving.

Share this post


Link to post
Share on other sites

Here's a simple example of what you could do, shouldn't be to hard to understand but if you can't understand it then just ask.

bool direction = false;

public void ballmove()
{
    if(direction = false){
        if(x >= 0)
            x++;
        else
            direction = true;
    }else{
        if(x <= 420)
            x--;
        else
            direction = false;
    }
}
 

EDIT: This kinda illustrates what Navall and thyagobr are talking about with the directions and stuff

Edited by xDarkShadowKnightx

Share this post


Link to post
Share on other sites
public float XVel = 1.0f;

public void BallMove()
{
    if (ball.X < 0 || ball.X > 420)
        XVel *= -1;

    ball.X += XVel;
}

 

Something more readable.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!