0
我有這個代碼創建一個ArrayList的所有像素位置實際上是一個像素(alpha!= 0)。在Slick的getColor()方法ArrayIndexOutOfBoundsException
的代碼如下:
public ArrayList<Point> getPixels() {
ArrayList<Point> output = new ArrayList<Point>();
Image frameImage = img.getCurrentFrame();
for (int FIx = 0; FIx <= img.getWidth(); FIx++) {
for (int FIy = 0; FIy <= img.getHeight(); FIy++) {
if (frameImage.getColor(FIx, FIy).getAlpha() != 0.00f) {//<-- Error
output.add(new Point(FIx, FIy));
}
}
}
return output;
}
環路可以完成精細幾次,都沒有任何錯誤,但在所謂隨機時間是跑,它提供了以下錯誤:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32768
at org.newdawn.slick.Image.getColor(Image.java:1248)
at com.SourCherry.games.KittySniper.Enemy.Kitten.getPixels(Kitten.java:197)
我已經標註了提到的一行(Kitten.java:197)和一條評論。
如果需要其他任何東西來幫助解決此問題,請在評論中提問。謝謝。
@njallam:我不明白這個問題......以前你的循環邊界是不正確的,因此當'FIy'的值太高時,就會出現異常......現在你已經修復了它,所以它不會拋出。 – 2012-07-17 13:08:06
@njallam:聽起來像它 - 或者可能是有問題的Image類有時會分配一個太大的數組,而且它沒有驗證參數意味着你一直在逃避它 - 但是獲取垃圾。 – 2012-07-17 13:47:53
感謝您的幫助。 – njallam 2012-07-17 15:45:41