eclipse - Java game ArrayIndexOutOfBounds -
how fix error? trying import "sub- sprite sheet" game getting arrayindexoutofbounds error
source code , errors:
i'm trying implement 'sub-spritesheet" , have no idea why i'm getting out of bounds error. here screens of class errors occur.
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 @ com.apcompsci.game.graphics.sprite.<clinit>(sprite.java:11) @ com.apcompsci.game.entity.mob.spider.<init>(spider.java:11) @ com.apcompsci.game.level.spawnlevel.loadlevel(spawnlevel.java:36) @ com.apcompsci.game.level.level.<init>(level.java:31) @ com.apcompsci.game.level.spawnlevel.<init>(spawnlevel.java:16) @ com.apcompsci.game.level.level.<clinit>(level.java:19) @ com.apcompsci.game.game.<init>(game.java:49) @ com.apcompsci.game.game.main(game.java:169) caused by: java.lang.arrayindexoutofboundsexception: 1 @ java.awt.image.bufferedimage.getrgb(unknown source) @ com.apcompsci.game.graphics.spritesheet.load(spritesheet.java:69) @ com.apcompsci.game.graphics.spritesheet.<init>(spritesheet.java:60) @ com.apcompsci.game.graphics.spritesheet.<clinit>(spritesheet.java:17) ... 8 more
my guess following line in bufferedimage failing
rgbarray[off++] = colormodel.getrgb(raster.getdataelements(x, y, data));
rgbarray array passed in function. needs quite big function initialise if pass in null.
try replacing
image.getrgb(0,0,w,h,pixels,0,w);
with
pixels = image.getrgb(0,0,w,h,null,0,w);
Comments
Post a Comment