1

スライディングブロックゲームで問題が発生しました。2つのブロックを高速で突進しようとすると、衝突コードが台無しになります。ただし、マウスの速度が遅い場合は機能します。マウスの速度に最大の上限を設定したり、コードを変更してそれが起こらないようにする方法はありますか?

    //when the mouse is dragged and component selected is not null, continue
    if (componentName != null) {
        //get the current mouse x and y assuming that it was clicked from the middle of the component
        mouseX = e.getX() - carImage[Integer.parseInt(componentName)].getWidth() / 2;
        mouseY = e.getY() - carImage[Integer.parseInt(componentName)].getHeight() / 2;
        //get the direction of the selected component
        direction = group.get(mapsIndex)[Integer.parseInt(componentName)].updown(Integer.toString(Integer.parseInt(componentName)));
        //if the direction is horizontal, make sure that the object is not dragged off the screen (right and left)
        if (direction == true) {
            if (mouseX < 50) {
                mouseX = 50;
            } else if (mouseX > 50 * 7 - carImage[Integer.parseInt(componentName)].getWidth()) {
                mouseX = 50 * 7 - carImage[Integer.parseInt(componentName)].getWidth();
            }
            //get the location of the mouse for the y axis
            mouseY = carImage[Integer.parseInt(componentName)].getY();
            //if the direction is vertical, make sure that the object is not dragged off the top and bottom of the screen
        } else {
            if (mouseY < 50) {
                mouseY = 50;
            } else if (mouseY > 50 * 7 - carImage[Integer.parseInt(componentName)].getHeight()) {
                mouseY = 50 * 7 - carImage[Integer.parseInt(componentName)].getHeight();
            }
            //get the location of the mouse for the x axis
            mouseX = carImage[Integer.parseInt(componentName)].getX();
        }
        //find the area that the selected object occupies
        Rectangle or = carImage[Integer.parseInt(componentName)].getBounds();
        //go through all other components
        for (int x = 0; x < max; x++) {
            //as long as the comparison is not made with itself or a nonexistant object, continue
            if (x != Integer.parseInt(componentName) && carImage[x] != null) {
                //get the area that the compared object occupies
                Rectangle collide = carImage[x].getBounds();
                //if the two areas intersect, make the selected car go back to where it was
                if (or.intersects(collide)) {
                    mouseX = carImage[Integer.parseInt(componentName)].getX();
                    mouseY = carImage[Integer.parseInt(componentName)].getY();
                }
            }
        }

        //update the component's location to where the mouse is
        carImage[Integer.parseInt(componentName)].setLocation(mouseX, mouseY);
    }
}
4

1 に答える 1

1

マウスの速度を制限しようとする道には行きません。ユーザーには個人的な好みがあります。

できることは、ユーザーの操作をシステムの動作から分離して、信頼性を高めることです。現在の位置を保持して更新し、指定された間隔でバックグラウンド スレッドで衝突を計算します。衝突が発生した場合は、ユーザーを戻します。ユーザーが気付くよりも短い間隔で実行できるはずです。また、より多くのコアを利用することでパフォーマンスを向上させることができます。つまり、Java 7 ForkJoin プールは単純なソリューションです。

于 2013-01-20T17:07:54.427 に答える