scalafx.animation.Timeline not working as expected

2019-08-10 20:42发布

问题:

I've started to try out the ScalaFX API few days ago. To learn the usage of this API I'm looking at the examples on GitHub. For testing out the features of the TimeLine class I used this example: ScalaFXAnimation.

The code to define a TimeLine object is looking like this in the example:

val timeline = new Timeline {
  cycleCount = Timeline.Indefinite
  autoReverse = true
  keyFrames = Seq(
    at (2 s) {rect1.x -> 200d tween Interpolator.EASE_IN},
    at (4 s) {rect1.x -> 300d},
    at (3 s) {rect2.y -> 100d tween Interpolator.EASE_BOTH},
    at (4 s) {rect2.y -> 300d},
    at (4 s) {rect2.width -> 300d tween Interpolator.EASE_OUT}
  )
}

If I try to do this in my own project I recieve some compile errors like:

Error:(58, 5) not found: value cycleCount

The values autoReverse, keyFrames and s are also not found. I did not set up the project and its structure by myself but cloned an "Hello world"-project from GitHub: scalafx-hello-world. This project and compiled properly.

Could it be a bug in ScalaFX? Do you have any ideas how to solve this problem?

EDIT2: Complete Code

package hello

import scalafx.animation.{Timeline, Interpolator}
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafx.scene.paint.Color
import scalafx.scene.shape.Rectangle
import scalafx.Includes._
import scala.language.postfixOps

object ScalaFXHelloWorld extends JFXApp {

  val rect1 = new Rectangle {
    width = 100
    height = 200
    fill = Color.Red
  }

  val rect2 = new Rectangle {
    width = 200
    height = 120
    fill = Color.Green
  }

  val timeline = Timeline {
    cycleCount = Timeline.Indefinite
    autoReverse = true
    keyFrames = Seq(
      at (2 s) {rect1.x -> 200d tween Interpolator.EASE_IN},
      at (4 s) {rect1.x -> 300d},
      at (3 s) {rect2.y -> 100d tween Interpolator.EASE_BOTH},
      at (4 s) {rect2.y -> 300d},
      at (4 s) {rect2.width -> 300d tween Interpolator.EASE_OUT}
    )
  }

  timeline.play()
  stage = new PrimaryStage {
    scene = new Scene {
      content = List(rect1, rect2)
    }
  }
}

回答1:

In the latest version you are missing new in front of Timeline. It should be:

val timeline = new Timeline { 
   ... 
}


标签: scala scalafx