1

我正在开发一款类似于积木的游戏,我希望某些东西在被击中时从屏幕上消失。我已经完成了碰撞检测,代码如下:

        Rectangle r1 = new Rectangle(x,y, 100,25);
        Rectangle r2 = new Rectangle(120,15, 90,40);
        Rectangle r3 = new Rectangle(10,15, 90,40);
        Rectangle r4 = new Rectangle(230, 15, 90, 40);
        Rectangle r5 = new Rectangle(xb, yb, 30,29);
        g.setColor(Color.RED);
        g.fillRect(r1.x, r1.y, r1.width, r1.height);
        g.setColor(Color.YELLOW);
        g.fillRect(r2.x, r2.y, r2.width, r2.height);
        g.setColor(Color.YELLOW);
        g.fillRect(r3.x, r3.y, r3.width, r3.height);
        g.setColor(Color.BLUE);
        g.fillRect(r5.x, r5.y, r5.width, r5.height);
        g.setColor(Color.YELLOW);
        g.fillRect(r4.x, r4.y, r4.width, r4.height);
        if (r5.intersects(r1))
        {
            velxb = -velxb;
            velyb = -velyb; 
        }
        if (r5.intersects(r2))
        {
            g.drawString("Hello", 10, 10);
        }
        if (r5.intersects(r3))
        {
            g.drawString("Hello", 10, 10);
        }
        if (r5.intersects(r4))
        {
            g.drawString("Hello", 10, 10);
        }

如您所见,我制作了几个矩形并完成了碰撞检测。但是,当r5r2orr3或相交时r4,我希望它消失。

4

1 回答 1

2

绘画是一个破坏性的过程。每次paintXxx调用您的方法时,您都应该重建输出,即清除现有Graphics上下文并重新绘制您想要的所有内容。

你的引擎应该做出关于模型应该发生什么效果的所有决定,并且 UI 应该渲染模型......确实不建议在你的绘画过程中进行碰撞检测

于 2013-09-02T06:16:10.053 回答