Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualj-locke

Posted 14 August 2012 - 08:24 PM

Let's go a little more indepth with what goes on for that method. For the time being we'll treat it like you only have 1 of them and it's named randomBall.

In your updateGame method you could have this code:

[source lang="java"]//This line has randomBall create and return a Rectangle that encloses it ('it' being the one ball//that we have stored in the variable randomBall)Rectangle randomBallRectangle = randomBall.getBallBounds();Rectangle mainBallRectangle = mainBall.getBallBounds();if (mainBallRectangle.intersects(randomBallRectangle)){ //Do relevant stuff for a collision}[/source]

It's in our updateGame method so that on every pass through our updateGame method, we get new rectangles from mainBall and randomBall. That means if they've moved, we're still getting up to date information because we're asking for it again every update before making the comparison.

Also note that I declared both of those Rectangle variables here inside the updateGame method. That means when the updateGame method ends, these variables are gone (they've gone out of scope) and then they are created anew next time through the updateGame method. This works in this particular design, but if I wanted to do something like be able to draw those rectangles (maybe for debugging purposes, or whatever), this wouldn't work because they wouldn't exist anymore by the time I got to the paint method. To get that kind of functionality, I would declare the variables in the game class (but not in any method) and then just assign to them each time through the updateGame method.

A good design question for you to think about is when you get the rectangle of one of your multiple random balls, what do you want to do with it? Do you want to compare it immediately against the mainBallRectangle? Do you want to store it in an array so you can later go through that array of Rectangles to see if each collides with mainBallRectangle? Since the random balls don't move, do you want to get their Rectangle as soon as the ball is created and store it in an array (that would mean you wouldn't need to update each time through the updateGame method)? Do you have a different approach in mind?

#1j-locke

Posted 14 August 2012 - 08:21 PM

Let's go a little more indepth with what goes on for that method. For the time being we'll treat it like you only have 1 of them and it's named randomBall.

In your updateGame method you could have this code:

[source lang="java"]//This line has randomBall create and return a Rectangle that encloses it ('it' being the one ball//that we have stored in the variable randomBall)Rectangle randomBallRectangle = randomBall.getBallBounds();Rectangle mainBallRectangle = mainBall.getBallBounds();if (mainBallRectangle.intersects(randomBallRectangle)){ //Do relevant stuff for a collision}[/source]

It's in our updateGame method so that on every update loop of our game, we get new rectangles from mainBall and randomBall. That means if they've moved, we're still getting up to date information because we're asking for it again every update before making the comparison.

Also note that I declared both of those Rectangle variables here inside the updateGame loop. That means when the updateGame loop ends, these variables are gone (they've gone out of scope) and then they are created anew next time through the updateGame method. This working in this particular design, but if I wanted to do something like be able to draw those rectangles (maybe for debugging purposes, or whatever), this wouldn't work because they wouldn't exist anymore by the time I got to the paint method.

A good design question for you to think about is when you get the rectangle of one of your multiple random balls, what do you want to do with it? Do you want to compare it immediately against the mainBallRectangle? Do you want to store it in an array so you can later go through that array of Rectangles to see if each collides with mainBallRectangle? Since the random balls don't move, do you want to get their Rectangle as soon as the ball is created and store it in an array (that would mean you wouldn't need to update each time through the updateGame method)? Do you have a different approach in mind?

PARTNERS