Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (565)
Games in Android Showcase (151)
games submitted by our members
Games in WIP (606)
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 1275 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: 367
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.

ags1 (21 views)
2015-03-31 10:55:12

theagentd (13 views)
2015-03-27 23:08:20

wxwsk8er (54 views)
2015-03-20 15:39:46

Fairy Tailz (48 views)
2015-03-15 21:52:20

Olo (29 views)
2015-03-13 17:51:59

Olo (33 views)
2015-03-13 17:50:51

Olo (40 views)
2015-03-13 17:50:16

Olo (44 views)
2015-03-13 17:47:07

ClaasJG (61 views)
2015-03-10 11:36:42

ClaasJG (44 views)
2015-03-10 11:33:01
How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

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 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!