Java游戏ArrayIndexOutOfBounds

时间:2014-06-09 22:29:37

标签: java eclipse game-engine

如何修复此错误?我想在我的游戏中导入一个“子精灵表”,但我得到一个arrayIndexOutOFBounds错误

源代码和错误:

我正在尝试实现一个'子spritesheet',我不知道为什么我得到这个超出界限的错误。这是所有错误发生的类的屏幕。

package com.apcompsci.game.graphics;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
private String path;
public final int SIZE;
public final int WIDTH,HEIGHT;
public int [] pixels;

public static SpriteSheet tiles = new SpriteSheet("/textures/sheets/spritesheet.png",256);
public static SpriteSheet spawn_level = new SpriteSheet("/textures/sheets/spwan_level.png",48);
public static SpriteSheet projectile_DemiGod = new SpriteSheet("/textures/sheets/projectiles/DemiGod.png",48);
public static SpriteSheet player = new SpriteSheet("/textures/sheets/player_sheet.png",95,129);
public static SpriteSheet player_down = new SpriteSheet(player, 0, 0, 1,3,32);

public SpriteSheet(SpriteSheet sheet, int x, int y, int width, int height, int spriteSize)
{
    int xx =  x*spriteSize;
    int yy = y*spriteSize;
    int w = width*spriteSize;
    int h = height *spriteSize;
    if(width == height) SIZE = width;
    else SIZE = -1;
    WIDTH = w;
    HEIGHT = h;
    pixels = new int[w*h];
    for(int y0 = 0; y0<h; y0++)
    {
        int yp = yy + y0;
        for(int x0 = 0; x0<w; x0++)
        {
            int xp = xx + x0;
            pixels[(x0+y0)*w] = sheet.pixels[(xp+yp)*sheet.WIDTH];
        }
    }

}

public SpriteSheet(String path, int size)
{
    this.path = path;
    SIZE = size;
    pixels = new int[SIZE*SIZE];
    WIDTH = size;
    HEIGHT = size;
    load();
}

public SpriteSheet(String path, int width, int height)
{
    this.path = path;
    SIZE = -1;
    WIDTH = width;
    HEIGHT = height;
    pixels = new int[SIZE*SIZE];
    load();
}

private void load()
{
    try {
        BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
        int w = image.getWidth();
        int h = image.getHeight();
        image.getRGB(0,0,w,h,pixels,0,w);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.apcompsci.game.graphics.Sprite.<clinit>(Sprite.java:11)
    at com.apcompsci.game.entity.mob.spider.<init>(spider.java:11)
    at com.apcompsci.game.level.SpawnLevel.loadLevel(SpawnLevel.java:36)
    at com.apcompsci.game.level.Level.<init>(Level.java:31)
    at com.apcompsci.game.level.SpawnLevel.<init>(SpawnLevel.java:16)
    at com.apcompsci.game.level.Level.<clinit>(Level.java:19)
    at com.apcompsci.game.Game.<init>(Game.java:49)
    at com.apcompsci.game.Game.main(Game.java:169)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at java.awt.image.BufferedImage.getRGB(Unknown Source)
    at com.apcompsci.game.graphics.SpriteSheet.load(SpriteSheet.java:69)
    at com.apcompsci.game.graphics.SpriteSheet.<init>(SpriteSheet.java:60)
    at com.apcompsci.game.graphics.SpriteSheet.<clinit>(SpriteSheet.java:17)
    ... 8 more

2 个答案:

答案 0 :(得分:1)

这一行:

pixels[(x0+y0)*w] = sheet.pixels[(xp+yp)*sheet.WIDTH];

改为:

pixels[x0 + (y0 * w)] = sheet.pixels[xp + (yp * sheet.WIDTH)];

考虑WIDTH = w = 5且HEIGHT = y = 5.y0和x0必须小于5,所以使用你的代码它将尝试在像素数组中写入(4 + 4)* 5 = 40个元素,这是初始化为只有25个元素。

答案 1 :(得分:0)

我的猜测是BufferedImage中的以下行失败

rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x,
                                y,
                                data));

rgbArray是传入函数的数组。它需要非常大,但如果传入null,函数将为您初始化它。

尝试替换

image.getRGB(0,0,w,h,pixels,0,w);

pixels = image.getRGB(0,0,w,h,null,0,w);