I want to ask if why am getting error loading any sprite images into the object
here is how I get the image in.
import java.awt.image.BufferedImage;
import java.io.IOException;
public class SpriteSheet {
public BufferedImage sprite;
public BufferedImage[] sprites;
int width;
int height;
int rows;
int columns;
public SpriteSheet(int width, int height, int rows, int columns, BufferedImage ss) throws IOException {
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
this.sprite = ss;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
sprites[(i * columns) + j] = ss.getSubimage(i * width, j * height, width, height);
}
}
}
}
here is how I'm implementing it
public BufferedImage[] init(){
BufferedImageLoader loader = new BufferedImageLoader();
BufferedImage spriteSheet = null;
SpriteSheet ss = null;
try {
spriteSheet = loader.loadImage("planet.png");
ss = new SpriteSheet(72,72,4,5,spriteSheet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ss.sprites;
}
I also want to ask about my way of using the sprites array. I want to use in the timer by changing the image drawn by action event by changing the current sprite image
tmr = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(Rock r:rocks){
r.move();
r.changeSprite();
}
repaint();
}
});
with the method
public void changeSprite(){
if(ds==12)
ds=0;
ds++;
currentSprite = sprite[ds];
}
Each rock object has a sprite array full of Buffered image received from the sprite image loaded in and a current image which get drawn. the timer will change the current image and redrawn it on the object so that the whole sprite get drawn but it doesn't seem to work. So it is my loadingSpriteImage that have the problem or my way of drawing it causing the problem?
Okay, so there a lots of things we need to know.
count != rows * cols
) and possibly even the size of each spriteSo based on you image from a previous question...
Based on this, we know there are 5 columns, 4 rows but only 19 images. Now you could spend a lot of time, write lots of code for each possible sprite sheet, or you could try and commensalism some of those problems...
So, this is pretty basic, it's simply a list of images. The special part is the
getSprite
method, which takes a progression through the current animation cycle and returns an image based on the number of images you have available. This basically decouples the concept of time from the sprite and allows you to define the meaning of a "cycle" externally.Now, because the actual process of building a
SpriteSheet
involves a lot of possible variables, a builder would be a good idea...So, again, based on your sprite sheet, this means I could build a
SpriteSheet
using something like...but it gives me the power to build any number of
SpriteSheets
, all of which might be made up of different matricesBut now that we have a
SpriteSheet
, we need some way to animate them, but what we really need is some way to calculate the progression through a given cycle (let's say a second is a cycle), we could use a simple "engine", something like...Now, this is basically just a wrapper class for a Swing
Timer
, but what it does is it calculates the cycle progression for us. It also has niceActionListener
support, so we can be notified when a tick occursOkay, but how does that all work together?
Basically, given one or more
SpriteSheet
s and aSpriteEngine
, we can paint the sheets doing something like...Now, okay, that's pretty basic, but it gives an idea.
For entities you want to move (or rotate), I would create another class which contained that information AND the
spriteSheet
. This might contain a "paint" method or you could use the properties of the object to then paint the individual frames...Something like...
Which could be created using something like...
Note, I only created the sprite sheet once. This might require you to supply a random "offset" to the
AstroidEntity
which will change which frame it returns for a given progress value...A simple way might be to add...
to
SpriteSheet
, then inAstroidEntity
you could create an offsetSpriteSheet
using something like...Painting might be done using something like...
Basically, the key factor here is, try and decouple your code from a concept of "time".
For example, I changed the frames-per-second the engine was using to 60 and saw no change in the animation of the sprites, because it was working on the concept of a single cycle of time been 1 second. This would, however allow you to change the speed at which the objects moved relatively simply.
You could also set the engine up to have a concept of "cycle-length" and make it half a second or 5 seconds which would then affect the animation speed - but the rest of you code would remain unchanged!