我最近将我的应用程序上传到了Google Play商店Helicopter Combat 但是它时不时地蜷缩起来。我在这里得到了错误报告:
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at de.krissini.server.Animation.getImage(Animation.java:36)
提到的代码:
public class Animation {
private Bitmap[] frames;
private int currentFrame;
private long startTime, delay;
private boolean playedOnce;
public void setFrames(Bitmap[] frames){
this.frames = frames;
currentFrame = 0;
startTime = System.nanoTime();
}
public void setDelay(long d){
delay = d;
}
public void setFrame(int i){
currentFrame = i;
}
public void update(){
long elapsed = (System.nanoTime()-startTime)/1000000;
if(elapsed>delay){
currentFrame++;
startTime = System.nanoTime();
}
if(currentFrame == frames.length){
currentFrame = 0;
playedOnce = true;
}
}
public Bitmap getImage(){
return frames[currentFrame]; // line 36
}
public int getFrame(){
return currentFrame;
}
public boolean playedOnce(){
return playedOnce;
}
}
我理解错误,但我该如何预防呢? 我很感激每一个答案
答案 0 :(得分:1)
错误是由于数组长度与最大索引相同的误解造成的。如果长度从1开始,索引从0开始,则它们总是不同1.
改变这个:
if(currentFrame == frames.length ){
// your reset code
}
对此:
if(currentFrame == frames.length - 1){
// your reset code
}