Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  SpriteSheet loading class returns error when there seems to be none  (Read 2164 times)
0 Members and 1 Guest are viewing this topic.
Offline xXShadowCowXx

Senior Newbie

« Posted 2012-03-01 02:43:42 »

Class Here

Hi again,

This is a spritesheet loading class that gets a buffered image, the amount of tiles in the x and y directions and then splits the image into it's subimages and puts it into an appropriately sized array. Problem is, I start getting a rasterformatexception halfway through the first row, saying that (x + width) is outside the raster. Thing is, when I print the amount of space left on the x-axis for the picture, I'm only halfway through loading it. I've only tested it with a single spritesheet that is sized at 192x128. Every time I've looked at the class, everything seems in order. It runs fine for the first four, I'm even able to display the images it successfully loads (Albeit anything after the first image contains all the previous images).

I figure I must be leaving something out, since I'm new to using a BufferedImage, having used only ImageIcons before. So anyone with some more experience have a modification to the code that could help me out? Appreciate any help.

Edit: Ooops this version is the wrong one. Going to put the right one up in a sec.

Edit 2: Updated.
Offline UprightPath
« Reply #1 - Posted 2012-03-01 04:45:52 »

I'm going to try not to be mean here, but this is not well written code. At least, the for loop you have is very bad. It's difficult to read and understand which means that it's probably not doing what you think it is.

I think what's going on here, is that you never reset the i variable. So, after the first time i == tilesX, it will never trigger that if again and spill off the edge of the sheet.

A better way to do this would be as a nested set of for loops...

for(int i = 0, index = 0; i < tilesX; i++) {
for(int j = 0; j < tilesY; j++, index++) {
tiles[index] = sprites.getSubImag(i * sizeX, j * sizeY, (i + 1) * sizeX, (j+1) * sizeY);

Edit: Added a bit to my description of why it's bad. ^^;

Offline xXShadowCowXx

Senior Newbie

« Reply #2 - Posted 2012-03-01 05:06:54 »

Thanks for the honesty. You were right. It wasn't tripping my crappy fail safe.

Thanks UprightPath you helped me get it working.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

« Reply #3 - Posted 2012-03-01 05:07:56 »

I also agree the code is very badly written, this shows you're not really ready for graphics yet. Learn more Java and OO then when you feel you're comfortable enough with Java, jump into graphics.

Pages: [1]
  ignore  |  Print  

EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (898 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (811 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!