I am writing a bouncing ball class (I’m just starting to use Java) and it works but it looks like every time the ball hits an edge the containing box gets bigger
Code for the bounce trigger
int i = 0;
int j = 0;
int k = 0;
double x = 3;
double y = 3;
while(i < 200){
if(j == 0){
x += 2;
} else {
x -= 2;
}
if(k == 0){
y += 4;
} else {
y -= 4;
}
if(thisEye.getX() > 400){
j = 1;
}
if(thisEye.getX() < 0) {
j = 0;
}
if(thisEye.getY() > 200){
k = 1;
}
if(thisEye.getY() < 0) {
k = 0;
}
Color someShade = rGen.nextColor(); // var for the color of the eyes
Color someOtherShade = rGen.nextColor();// var for the color of the pupules
moveThis(thisEye,x,y,someShade); // Go over all the shapes with the same X,Y offset - var x,y
moveThis(thisPupul,x,y,someOtherShade);
moveThis(thisEye2,x,y,someShade);
moveThis(thisPupul2,x,y,someOtherShade);
moveThis(face,x,y,rGen.nextColor());
moveThis(nose,x,y,rGen.nextColor());
pause(150.0); // wait for next cycle to slow down the images
//i++; this was to see if it just got farther off with each loop, it dose.
}
}
private void moveThis(GOval subject, double x, double y, Color newShade){
subject.setFillColor(newShade);
subject.move(x, y);
}
the ‘face’ will bounce off the right spot once then each time it bounces it gets farther and farther off the screen tell is so far of the it only is on screen for a short time then off the other side tell it comes back on screen for a bit.
I marked it as home work but I’m a html/php coder in the day and am just using the iTunesU Stanford videos at night.
but I’m trying to learn so pointers would be great.
The problem could be (though very unlikely) with your theEye.getX() and theEye.getY() method. What if you change your boundary condition check from
TO
Otherwise, please post the complete source code so we can see where else could the problem be.