Java Contaning box似乎变得越来越大

时间:2021-08-29 00:18:35

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

我正在写一个弹跳球类(我刚开始使用Java)并且它有效但看起来每次球击中边缘时包含的盒子变得更大

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.

我把它标记为家庭作业,但我当天是一个html / php编码器,我只是在晚上使用iTunesU Stanford视频。但我正在努力学习,所以指针会很棒。

1 个解决方案

#1


0  

The problem could be (though very unlikely) with your theEye.getX() and theEye.getY() method. What if you change your boundary condition check from

使用theEye.getX()和theEye.getY()方法可能会产生问题(尽管不太可能)。如果您更改边界条件检查怎么办?

if(thisEye.getX() > 400){
    j = 1;
} 
if(thisEye.getX() < 0) {
    j = 0;
}
if(thisEye.getY() > 200){
    k = 1;
} 
if(thisEye.getY() < 0) {
    k = 0;
}

TO

if(x > 400){
    j = 1;
} 
if(x < 0) {
    j = 0;
}
if(y > 200){
    k = 1;
} 
if(y < 0) {
    k = 0;
}

Otherwise, please post the complete source code so we can see where else could the problem be.

否则,请发布完整的源代码,以便我们可以看到问题所在。

#1


0  

The problem could be (though very unlikely) with your theEye.getX() and theEye.getY() method. What if you change your boundary condition check from

使用theEye.getX()和theEye.getY()方法可能会产生问题(尽管不太可能)。如果您更改边界条件检查怎么办?

if(thisEye.getX() > 400){
    j = 1;
} 
if(thisEye.getX() < 0) {
    j = 0;
}
if(thisEye.getY() > 200){
    k = 1;
} 
if(thisEye.getY() < 0) {
    k = 0;
}

TO

if(x > 400){
    j = 1;
} 
if(x < 0) {
    j = 0;
}
if(y > 200){
    k = 1;
} 
if(y < 0) {
    k = 0;
}

Otherwise, please post the complete source code so we can see where else could the problem be.

否则,请发布完整的源代码,以便我们可以看到问题所在。