Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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 1242 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...

1  
2  
3  
4  
5  
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: 356
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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (14 views)
2014-12-28 10:39:27

CopyableCougar4 (18 views)
2014-12-28 02:10:29

BurntPizza (22 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org 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‑gaming.org
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!