Skip to content
Advertisement

Load a sprites image in java

I want to ask if why am getting error loading any sprite images into the object

here is how I get the image in.

JavaScript

here is how I’m implementing it

JavaScript

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

JavaScript

with the method

JavaScript

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?

Advertisement

Answer

Okay, so there a lots of things we need to know.

  • How many images make up the sprite sheet, how they are laid out (rows/cols), if there is an uneven number of images (count != rows * cols) and possibly even the size of each sprite
  • How far we are through a given cycle (let’s say a second)

So based on your image from a previous question…

Sprite Sheet

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…

JavaScript

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…

JavaScript

So, again, based on your sprite sheet, this means I could build a SpriteSheet using something like…

JavaScript

but it gives me the power to build any number of SpriteSheets, all of which might be made up of different matrices

But 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…

JavaScript

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 nice ActionListener support, so we can be notified when a tick occurs

Okay, but how does that all work together?

Basically, given one or more SpriteSheets and a SpriteEngine, we can paint the sheets doing something like…

Spinny

JavaScript

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…

JavaScript

Which could be created using something like…

JavaScript

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…

JavaScript

to SpriteSheet, then in AstroidEntity you could create an offset SpriteSheet using something like…

JavaScript

Painting might be done using something like…

JavaScript

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!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement