我知道Polyline sine = new Polyline();导致了该错误,因为它试图创建一个已经存在的东西的新类实例。如何清除每个关键帧的实例或在方法外部声明折线,而又不与它创建的正弦波一起创建对角线?
My Full Code

public BallOnCurvePane() {
        // Create an animation for moving the ball
        animation = new Timeline(
                new KeyFrame(Duration.millis(50), e -> moveBall()));
        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();
    }

protected void moveBall() {
        Polyline sine = new Polyline();
        ObservableList<Double> list = sine.getPoints();
        for (double x = -170.0; x <= 170.0; x++) {
            list.add(x + 200.0);
            list.add(80.0 - 50.0 * Math.sin((x / 100.0) * 2.0 * (double) Math.PI));
        }
        circle.setFill(ballColor);
        getChildren().addAll(sine, circle);

        pt.setRate(.25);
        pt.setPath(sine);
        pt.setNode(circle);
        pt.setCycleCount(Timeline.INDEFINITE);
        pt.setAutoReverse(true);
        pt.play();

    }


我的全部错误

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = BallOnCurvePane@13424ecf
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
    at BallOnCurvePane.moveBall(BallOnCurvePane.java:68)
    at BallOnCurvePane.lambda$new$0(BallOnCurvePane.java:25)
    at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:239)
    at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:180)
    at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
    at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
    at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:110)
    at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
    at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186)
    at java.security.AccessController.doPrivileged(Native Method)
    at javafx.animation.Animation$1.timePulse(Animation.java:185)
    at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
    at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

最佳答案

线

getChildren().addAll(sine, circle);


每次添加circle的相同实例。无需多次添加节点。
此外,由于折线仅代表球应走的路径,因此无需将其作为孩子添加(除非您想绘制路径并沿其移动圆)。

08-28 21:28