繁体   English   中英

Slick中getColor()方法的ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException on getColor() method in Slick

我有这个代码来创建一个ArrayList,其中包含实际上有像素的所有像素位置(alpha!= 0)。

代码如下:

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)。

如果需要其他任何方法来帮助解决此问题,请在评论中提问。 谢谢。

这看起来像我的问题:

for (int FIx = 0; FIx <= img.getWidth(); FIx++) {
    for (int FIy = 0; FIy <= img.getHeight(); FIy++) {

你假设它的像素在包含 getWidthgetHeight的范围内。 我强烈怀疑这些是排他性的上限:

for (int FIx = 0; FIx < img.getWidth(); FIx++) {
    for (int FIy = 0; FIy < img.getHeight(); FIy++) {

因此,例如,宽度为3的图像应具有0,1和2的有效X值 - 而不是 3。

不可否认,这完全取决于org.newdawn.slick.Image作用,这是我不熟悉的一个类 - 但这是一个合理的起点。 (显然该方法不验证其输入是一种遗憾 - 它应该抛出一个不同的异常,但它仍然是你的编程错误。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM