Libgdx 什么';Kotlin中get和[]调用之间的区别是什么?
我正在使用Kotlin语言+LibGDX库制作游戏 Kotlin版本:Libgdx 什么';Kotlin中get和[]调用之间的区别是什么?,libgdx,kotlin,Libgdx,Kotlin,我正在使用Kotlin语言+LibGDX库制作游戏 Kotlin版本:1.1.2-4 LibGDX版本:1.9.6 正如官方文件所说 "[] operation stands for calls to member functions get() and set()." 但是,当我尝试运行这行代码时: body.userData=animations[state[e].currentState]!!。关键帧[帧] 我得到这个错误: 线程“LWJGL应用程序”java.lang.ClassCa
1.1.2-4
LibGDX版本:1.9.6
正如官方文件所说
"[] operation stands for calls to member functions get() and set()."
但是,当我尝试运行这行代码时:
body.userData=animations[state[e].currentState]!!。关键帧[帧]
我得到这个错误:
线程“LWJGL应用程序”java.lang.ClassCastException中的异常:[Ljava.lang.Object;无法强制转换到[Lcom.badlogic.gdx.graphics.g2d.TextureRegion;
但是,当我将[]更改为get()时:
body.userData=animations[state[e].currentState]!!.keyFrames.get(frame)
一切都会好起来的
PS:“帧”是角度,转换为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”是对Ashley的实体引用,“body”、“anim”和“state”是Ashley组件映射器
对于那些不知道LibGDX是什么的人来说,这里是动画课
这里的关键帧只是一个Java数组
UPD 2:
我注意到只有在使用此构造函数时才会出现问题:
Animation(float frameDuration, Array<? extends T> keyFrames)
动画(浮动帧持续时间,数组投射错误对象不能投射到纹理区域
public operator fun get(index: Int): T
返回指定索引为kotlin.array
的数组元素
T[]关键帧
是动画
类中类型为T的数组
我以这种方式声明,我没有铸造错误/异常:
lateinit var ani:Animation<TextureRegion>
ani = Animation(.1f,TextureRegion(Texture("badlogic.jpg")))
val y= ani.keyFrames.get(0) // -> y is a TextureRegion
然后将userData分配给body
val world = World(Vector2(10f,10f),false)
val body = world.createBody(BodyDef())
body.userData = y // -> Either you use x or y you always get sprite
var sprite=Sprite(body.userData as TextureRegion?)
投射错误对象无法投射到TextureRegion
public operator fun get(index: Int): T
返回指定索引为kotlin.array
的数组元素
T[]关键帧
是动画
类中类型为T的数组
我以这种方式声明,我没有铸造错误/异常:
lateinit var ani:Animation<TextureRegion>
ani = Animation(.1f,TextureRegion(Texture("badlogic.jpg")))
val y= ani.keyFrames.get(0) // -> y is a TextureRegion
然后将userData分配给body
val world = World(Vector2(10f,10f),false)
val body = world.createBody(BodyDef())
body.userData = y // -> Either you use x or y you always get sprite
var sprite=Sprite(body.userData as TextureRegion?)
关键帧的类型是什么?您使用的是哪个LibGDX版本?@AbhishekAryan 1.9。6@GrzegorzPiwowarek@haxpor我在一个循环中测试了它100次,当我改为get()时,得到了100个异常,而不是0个异常关键帧的类型是什么?您使用的是哪个LibGDX版本?@AbhishekAryan 1.9。6@GrzegorzPiwowarek@haxpor我在一个循环中测试了100次,得到了100个异常,而当我改为get()时,得到了0个异常。我真的不明白你的意思。我甚至在这行“val texture=animations[state[e].currentState]”中也得到了异常。keyFrames[frame]”。我不认为我在这里铸造了什么。什么是动画?是动画数组吗?和状态[e]。当前状态?动画是HashMap()state是Ashley的组件映射器。currentState是字符串,但我不认为这对soI有帮助。我不认为问题出在上面的行中,可能是您正在访问其他系统。我实际上不理解您的意思。即使在这一行中,我也遇到了异常“val texture=animations[state].currentState]!!.keyframe[frame]”。我不认为我在这里铸造了什么。什么是动画?是动画数组吗?和状态[e]。currentState
?动画是HashMap(),状态是Ashley的组件映射器。currentState是字符串,但我认为这对soI没有帮助。我不认为问题在上面,可能是您正在访问其他系统