我使用Kotlin語言+ LibGDX庫製作遊戲。Kotlin中的get和[]調用有什麼區別?
科特林版本:1.1.2-4
LibGDX版本:1.9.6
官方文件說
"[] operation stands for calls to member functions get() and set()."
但是,當我嘗試運行這行代碼:
body.userData = animations[state[e].currentState]!!.keyFrames[frame]
我得到這個錯誤:
Exception in thread "LWJGL Application" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;
然而,當我改變[]獲得():
body.userData = animations[state[e].currentState]!!.keyFrames.get(frame)
一切都得到確定。
PS:「frame」是角度,轉換爲Int。
UPD: 我改變了我的代碼有點
val animation: Animation = anim[e].animations[state[e].currentState]!!
val tex = animation.keyFrames[frame] // Get exception on this line anyway
body[e].body.userData = tex
「E」是阿什利實體refrence。 「body」,「anim」和「state」是Ashley組件映射器。
對於那些不知道LibGDX在這裏的人是動畫類 這裏的keyFrames只是一個Java Array。
UPD 2: 我注意到,只有當我使用這個構造發生這個問題:
Animation(float frameDuration, Array<? extends T> keyFrames)
貌似互操作問題。
'keyFrames'的類型是什麼? –
您正在使用哪種LibGDX版本? – Aryan
@AbhishekAryan 1.9。6 – icarumbas