2 o1101574955 o1101574955 于 2016.02.23 15:59 提问

javafx让小球从上次静止的位置开始运行? 1C

有一个BallPane类,

 package javafx_ex;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class BallPane extends Pane{
    public final double radius=20;
    private double x=radius,y=radius;
    private double dx=1,dy=1;
    private Circle circle=new Circle(x,y,radius);
    private Timeline animation;
    public BallPane(){
        circle.setFill(Color.GREEN);
        getChildren().add(circle);
        animation=new Timeline(new KeyFrame(Duration.millis(50),e->moveBall()));
        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();
    }

    public void play(){
        animation.play();
    }

    public void pause(){
        animation.pause();
    }

    public void increaseSpeed(){
        animation.setRate(animation.getRate()+1);
    }

    public void decreaseSpeed(){
        animation.setRate(animation.getRate()>0 ? animation.getRate()-1 : 0);
    }

    public DoubleProperty rateProperty(){
        if(animation.getRate()==0.0){
            circle=new Circle(circle.getCenterX(),circle.getCenterY(),radius);
            circle.setFill(Color.GREEN);
        }
        return animation.rateProperty();
    }

    protected void moveBall(){
        if(x<radius || x>getWidth()-radius){
            dx*=-1;
        }
        if(y<radius || y>getHeight()-radius){
            dy*=-1;
        }
        x+=dx;
        y+=dy;
        circle.setCenterX(x);
        circle.setCenterY(y);
    }
}

主函数,

 package javafx_ex;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;

public class BounceBallSlider extends Application{
    @Override
    public void start(Stage primaryStage){
        BallPane ballPane=new BallPane();

        Slider slSpeed=new Slider();
        slSpeed.setMax(20);
        slSpeed.setValue(10);
        ballPane.rateProperty().bind(slSpeed.valueProperty());

        BorderPane pane=new BorderPane();
        pane.setCenter(ballPane);
        pane.setBottom(slSpeed);

        ballPane.setOnMousePressed(e->ballPane.pause());
        ballPane.setOnMouseReleased(e->ballPane.play());
        slSpeed.setOnKeyPressed(e->{
            if(e.getCode()==KeyCode.UP){
                slSpeed.setValue(slSpeed.getValue()+1);
            }
            else if(e.getCode()==KeyCode.DOWN){
                slSpeed.setValue(slSpeed.getValue()-1);
            }
        });

        Scene scene=new Scene(pane,250,150);
        primaryStage.setTitle("BounceBallControl");
        primaryStage.setScene(scene);
        primaryStage.show();

        slSpeed.requestFocus();
    }
    public static void main(String[] args){
        Application.launch(args);
    }
}

出现的问题是:小球速度降为0后,再增加速度,小球不是从上次静止的位置开始运动,如果让它从上次静止的位置开始运动呢?
我查看了Animation.class的源代码,里面有这样一句话,

 An {@code Animation}'s play head can be randomly positioned, whether it is
 * running or not. If the {@code Animation} is running, the play head jumps to
 * the specified position immediately and continues playing from new position.
 * If the {@code Animation} is not running, the next {@link #play()} will start
 * the {@code Animation} from the specified position.

这句话是什么意思呢?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!