jeudi 30 novembre 2017

Strange java.lang.ClassCastException when using [] call.

I'm using LibDGX library for my game. And i faced common exception ClassCastException, but it occurs in strange case.

I'm using Animation class from LibGDX library.

I'm getting error on this line only if i am using [] operation.

val tex = animation.keyFrames[0]

If i change it to get() error disappears.

tex = animation.keyFrames.get(0)

Here is full method. I simplified example.

override fun create() {


    val frames = Array<TextureRegion>()
    frames.add(TextureRegion(Texture("badlogic.jpg")))

    val animation = Animation(frames)

    val tex1 = animation.keyFrames.get(0)  // Compiles

    val tex = animation.keyFrames[0]  // error - [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;


}

Strange, isn't it?

Here is Animation class that i used to minimise other error reasons. It is exactly the same as in LibGDX api.

    public class Animation<T> {

    T[] keyFrames;


    public Animation (Array<? extends T> keyFrames) {

        Class arrayType = keyFrames.items.getClass().getComponentType();
        T[] frames = (T[]) ArrayReflection.newInstance(arrayType, keyFrames.size);
        for (int i = 0, n = keyFrames.size; i < n; i++) {
            frames[i] = keyFrames.get(i);
        }

        setKeyFrames(frames);
    }

    protected void setKeyFrames (T... keyFrames) {
        this.keyFrames = keyFrames;
    }
}

I also noticed that error disappears if i use Array.with construction instead of creating Array<TextureRegion>

val animation = Animation(Array.with(TextureRegion(Texture("badlogic.jpg"))))

Could you tell me the reason why this occurs or it is possible a bug?





Aucun commentaire:

Enregistrer un commentaire