0

我正在写一个像命运之轮这样的游戏,轮子实际上是一个 JavaFX ImageView,位于窗口的最右侧。这个轮子由 15 片组成(每个 24 度)。

车轮部分

基本上,在 RotateTransition 完成后,我想获取 ImageView 的坐标或边界框,以便计算该坐标、图像中心和针之间的角度。从那个角度我可以确定返回的分数。

但是,我对如何做到这一点感到困惑。我试过玩边界框,但它并没有解决问题。

代码如下:

public class FXMLRoundSceneController {
    @FXML
    private AnchorPane backgroundAnchorPane;

    @FXML
    private ImageView wheel;

    /* Wheel background Image*/
    private Image img = new Image(getClass().getResource("/resources/WOF.png").toExternalForm());

    @FXML
    public void initialize() {
        wheel.setImage(img);
    }   

    @FXML
    private void rotateWheel(MouseEvent mv) {
        rotate(1355,5);     // Just an example, the rotate angle should be randomly calculated on each MouseClicked event.
    }

    public void rotate(double angle, double duration) {
        RotateTransition rt = new RotateTransition();
        rt.setNode(wheel);
        rt.setByAngle(angle);
        rt.setDuration(Duration.seconds(duration));
        rt.setCycleCount(1);
        rt.setAutoReverse(false);
        rt.setInterpolator(Interpolator.EASE_OUT);
        rt.play();

        rt.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ae) {
                wheelBoundingBox();
            }
        });
    }

    public void wheelBoundingBox() {
        double MX = wheel.getBoundsInParent().getMaxX();
        double MY = wheel.getBoundsInParent().getMaxY();
        double mX = wheel.getBoundsInParent().getMinX();
        double mY = wheel.getBoundsInParent().getMinY();

        double width = MX - mX;
        double height = MY - mY;

        Rectangle bb = new Rectangle(mX, mY, Color.TRANSPARENT);
        bb.setWidth(width);
        bb.setHeight(height);
        bb.setStroke(Color.WHITE);
        bb.setStrokeWidth(1);

        backgroundAnchorPane.getChildren().add(bb);
    }
}

谁能告诉我确定坐标的正确方法或类似的方法来帮助您确定分数。

4

1 回答 1

-1
public static void main(String[] args)  {
    String[] prizes = new String[] {"100","200","300","400","500","600"};
    int slotSize = 360 / prizes.length;
    int angle = 359;
    for (int i = 0; i < 3600; i++) // 10 spins
    {
        angle++;
        if (angle>359) {
            angle = 0;
        }
        // based on the current angle, determine the entry:
        int entry = angle / slotSize;
        System.out.println("angle "+angle+" entry "+entry+" prize "+prizes[entry]);
    }
}
于 2014-05-21T17:04:05.403 回答