coder4life22
coder4life22

Reputation: 31

ArrayIndexOutOfBoundsException with sprite sheet

I keep getting this error with andengine when using a sprite sheet.

Can anyone explain what i means?

08-30 13:31:50.053: E/AndroidRuntime(9643): java.lang.ArrayIndexOutOfBoundsException: length=12; index=12

here is where the error is occuring at.

    pItem.setCurrentTileIndex(MathUtils.random(0, pItem.getTileCount()));

Upvotes: 0

Views: 166

Answers (2)

Brian
Brian

Reputation: 17309

My guess is that MathUtils.random is inclusive, which means that in your example (array of size 12), it will generate a number from 0 to 12, including 12. Try changing your code to:

pItem.setCurrentTileIndex(MathUtils.random(0, pItem.getTileCount() - 1));

Upvotes: 1

Austin Greco
Austin Greco

Reputation: 33544

try: pItem.setCurrentTileIndex(MathUtils.random(0, pItem.getTileCount()-1));

getTileCount() likely returns the # of tiles, so indexes would be that minus 1.

Upvotes: 1

Related Questions