Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

arrays - LibGDX Animation Java to Kotlin

I am trying to convert this LibGDX example on Animation using a sprite sheet from Java to Kotlin

Here is the link to the Java code snippet: https://github.com/libgdx/libgdx/wiki/2D-Animation

Here is my Kotlin code:

 // load the sprite sheet as a texture
    idleSheet = Texture("raw/Skeleton-Idle.png".toInternalFile())
    idleFrames = Array<TextureRegion>(true, FRAME_ROWS*FRAME_COLS)

    // use split method to create 2d array of texture regions
    // sprite sheet contains frames of equal size & alignment
    var temp = TextureRegion.split(
        idleSheet,
        idleSheet.width / FRAME_COLS,
        idleSheet.height / FRAME_ROWS
    )

    var index = 1
    for (i in 1..FRAME_ROWS) {
        for (j in 1..FRAME_COLS) {
             idleFrames[index] = temp[i][j]
            index ++
        }
    }
    // initialise animation with frame interval & array of frames
    idleAnimation = Animation(0.05f,idleFrames)

The problem seems to be Index 1 out of bounds for length 1 in line idleFrames[index] = temp[i][j] - I am doing something very basic wrong with Kotlin Arrays but can't seem to fix it.

Any ideas?

question from:https://stackoverflow.com/questions/65904913/libgdx-animation-java-to-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. .. is end inclusive. until is end exclusive.

  2. Arrays start at index 0, not 1. So use i in 0 until FRAME_ROWS, for example.

  3. The libGDX Array class behaves a lot like a List. You can't directly assign values to indices that haven't been filled yet. So instead of idleFrames[index] = temp[i][j], use idleFrames.add(temp[i][j]) and you can get rid of the index variable entirely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...