我開始使用 Scala 進行編程,并決定使用 libgdx 制作一個非常簡單的游戲。我創建了這個類:
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.{GL20, Texture}
import com.badlogic.gdx.Gdx
import com.badlogic.gdx
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.TextureRegion
class Game extends gdx.Game {
var batch: SpriteBatch = _
var walkSheet: Texture = _
var walkAnimation: Animation[TextureRegion] = _
var reg: TextureRegion = _
var stateTime: Float = _
def create(): Unit = {
batch = new SpriteBatch()
walkSheet = new Texture(Gdx.files.internal("assets/animations/slam with VFX.png"))
val frames: Array[TextureRegion] = TextureRegion.split(walkSheet, walkSheet.getWidth / 1, walkSheet.getHeight / 10).flatMap(_.toList)
walkAnimation = new Animation(1f/4f, frames)
}
override def render(): Unit = {
Gdx.gl.glClearColor(1f,1f,1f,1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
batch.begin()
stateTime = Gdx.graphics.getDeltaTime()
//println(stateTime)
reg = walkAnimation.getKeyFrame(stateTime, true)
batch.draw(reg, 0,0,reg.getRegionWidth*4, reg.getRegionHeight*4)
batch.end()
}
}
(對不起,我正在嘗試一些事情)
如您所見,幀的型別是 Array[TextureRegion]。在 libgdx Animation Class的檔案中,我可以看到我應該能夠使用這種型別呼叫 Animation 的建構式,但是 intellij 輸出:
[...]\git\master\src\Main\scala\Game.scala:21:42
type mismatch;
found : Array[com.badlogic.gdx.graphics.g2d.TextureRegion]
required: com.badlogic.gdx.graphics.g2d.TextureRegion
walkAnimation = new Animation(1f/4f, frames)
我不知道發生了什么。對于我的(有限的)理解,它似乎正在嘗試使用建構式的不同多載,但我不知道為什么也不知道如何解決它。
uj5u.com熱心網友回復:
是的,Scala 編譯器在這里很困惑,因為建構式想要一個 gdx 陣列,而不是 Java 陣列(查看檔案)。可能的解決方法是使用 var arg 語法:
walkAnimation = new Animation(1f/4f, frames:_*)
這樣,它選擇了這個建構式:
Animation(float frameDuration, T... keyFrames)
或者,如果您愿意,可以直接使用 GDX 陣列類:
import com.badlogic.gdx.utils.{Array => GDXArray}
val frames: GDXArray[TextureRegion] = new GDXArray(TextureRegion.split(walkSheet, walkSheet.getWidth / 1, walkSheet.getHeight / 10).flatMap(_.toList))
walkAnimation = new Animation(1f/4f, frames)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369098.html
上一篇:如何在Scala中正確處理回傳
下一篇:org.apache.spark.sql.AnalysisException:流式資料幀/資料集不支持非基于時間的視窗;;盡管有基于時間的視窗
