1

几天前我已经开始试用 ScalaFX API。要了解此 API 的用法,我正在查看GitHub 上的示例。为了测试这个TimeLine类的特性,我使用了这个例子:ScalaFXAnimation

示例中定义 TimeLine 对象的代码如下所示:

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}
  )
}

如果我尝试在自己的项目中执行此操作,我会收到一些编译错误,例如:

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

autoReverse,keyFramess也找不到。我没有自己设置项目及其结构,而是从 GitHub 克隆了一个“Hello world”项目:scalafx-hello-world。该项目并正确编译。

会不会是 ScalaFX 中的错误?您对如何解决这个问题有任何想法吗?

EDIT2:完整代码

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)
    }
  }
}
4

1 回答 1

2

在最新版本中,您new前面缺少Timeline. 它应该是:

val timeline = new Timeline { 
   ... 
}
于 2015-02-20T21:07:20.873 回答