2017-04-01 100 views
0

我正在嘗試製作滾動背景;希望將它連接到尾部,這樣它就可以旋轉相同的「色帶」,並將其放置在我的遊戲後部。JavaFX動畫慢動作圖像

我一直在尋找實現動畫效果的正確方法。我可以按照我的示例或TimeLine使用AnimationTimer。但是,我想要將動畫速度移得更慢,並延遲定時器的更新會導致跳動。我添加了一個int變量,如果小於「x」millis已經過去了,它會返回,但它看起來不太好。

什麼是更好的方法來完成這項任務?

import javafx.animation.AnimationTimer; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.image.Image; 
import javafx.scene.layout.*; 
import java.io.File; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

class ImageContainer extends VBox { 
    int w, h; 
    int sectionScrollWidth = 1; 
    int sections; 
    int sectionCounter = 0; 
    Image image; 
    Canvas canvas; 

public ImageContainer() { 
    setVisible(true); 
    load(); 
    w = (int) image.getWidth(); 
    h = (int) image.getHeight(); 
    canvas = new Canvas(w, h); 
    getChildren().add(canvas); 
    sections = w/sectionScrollWidth; 
    GraphicsContext gc = canvas.getGraphicsContext2D(); 
    canvas.setVisible(true); 
    gc.drawImage(image, 0, 0, w, h); 
    setPrefSize(w, h); 

    final long startNanoTime = System.nanoTime(); 

    new AnimationTimer() { 
     public void handle(long currentNanoTime) { 
      sectionCounter = sectionCounter - sectionScrollWidth; 
      canvas.setTranslateX(sectionCounter); 
     } 
    }.start(); 

//  KeyValue kv1 = new KeyValue(canvas.translateXProperty(), 0); 
//  KeyValue kv2 = new KeyValue(canvas.translateXProperty(), 2000); 
//  KeyFrame kf1 = new KeyFrame(Duration.millis(3000), kv1, kv2); 
//  Timeline translate = new Timeline(); 
//  translate.getKeyFrames().add(kf1); 
//  translate.play(); 

} 

public void load() { 
    Path imagePath = Paths.get("./src/main/resources/ribbonImages/clouds.png"); 
    File f = imagePath.toFile(); 
    assert f.exists(); 
    image = new Image(f.toURI().toString()); 
} 
} 

回答

0
if (((System.nanoTime()-startNanoTime)/1000000000.0)<0.05) { 
       return; 
      } 
      else { 
       startNanoTime = System.nanoTime(); 
      } 

添加到handle()方法將迫使它返回,如果小於0.05秒已經過去了。相當明顯的答案,但我不想刪除我自己的問題,以防萬一這可以幫助別人。