8 Replies - 4959 Views - Last Post: 22 April 2011 - 12:16 PM Rate Topic: -----

#1 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Best way to animate a sprite?

Posted 15 April 2011 - 06:04 PM

Hey guys, I was working on a little java game. At the moment it involved monster objects spawning at various places on the window and they swarm towards the player. It is the players goal to avoid them for as long as possible simply by running away or attacking. Now so far all of my sprite movement animations are achieved simply by changing the sprite's image to a different gif animation (i.e., if the player is walking East change to sprite_walk_eat.gif). I think this is fine, however, I am now attempting to animate and I don't think this would be the best way to accomplish it.

What algorithms do you all use when performing sprite animations?

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to animate a sprite?

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Best way to animate a sprite?

Posted 15 April 2011 - 06:19 PM

I use sprite sheets when I animate in a game, so instead of loading a whole new image (which can be troublesome if you have many entities on the screen all changing) you just change some offsets and it will then blit the part of the image at that point. I have not used java so I am not quite sure how it would be done this way, but it can't be too difficult :)

an example of such a spritesheet is this: doom spritesheet

That's what I do anyway :)
Was This Post Helpful? 0
  • +
  • -

#3 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Re: Best way to animate a sprite?

Posted 15 April 2011 - 07:26 PM

View Postsk1v3r, on 15 April 2011 - 06:19 PM, said:

I use sprite sheets when I animate in a game, so instead of loading a whole new image (which can be troublesome if you have many entities on the screen all changing) you just change some offsets and it will then blit the part of the image at that point. I have not used java so I am not quite sure how it would be done this way, but it can't be too difficult :)

an example of such a spritesheet is this: doom spritesheet

That's what I do anyway :)


I've seen a few people using sprite sheets, interesting, I'll have to look into this.
Was This Post Helpful? 0
  • +
  • -

#4 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Best way to animate a sprite?

Posted 22 April 2011 - 08:53 AM

Here's a decent example of what you are trying to accomplish :

Java Spritesheet Code

The concept is universal to any language really, so I'd focus on understanding the concept. Keep in mind that it does require the sprites to have standard dimensions.
Was This Post Helpful? 0
  • +
  • -

#5 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Re: Best way to animate a sprite?

Posted 22 April 2011 - 09:04 AM

View PostPrototypical, on 22 April 2011 - 08:53 AM, said:

Here's a decent example of what you are trying to accomplish :

Java Spritesheet Code

The concept is universal to any language really, so I'd focus on understanding the concept. Keep in mind that it does require the sprites to have standard dimensions.


I tried that, didn't work out quite as I anticipated, however I'm now using Slick 2D to animate my spritesheets and it's working fantastic!
Was This Post Helpful? 0
  • +
  • -

#6 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Best way to animate a sprite?

Posted 22 April 2011 - 10:41 AM

Great, but the concept is pretty universal. You probably didn't take note that the spritesheet the user posted, had sprites of different dimensions -- it's imperative that the sprites are of the same dimensions.

It's basically just grabbing a region from a bitmap and storing as a frame. The math relies on the sprites being uniform.

I would definitely encourage you to understand the concepts involved, cause using code that you don't understand can lead to problems. If you understand the concept, you can apply it in any language.

This post has been edited by Prototypical: 22 April 2011 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Re: Best way to animate a sprite?

Posted 22 April 2011 - 11:55 AM

View PostPrototypical, on 22 April 2011 - 10:41 AM, said:

Great, but the concept is pretty universal. You probably didn't take note that the spritesheet the user posted, had sprites of different dimensions -- it's imperative that the sprites are of the same dimensions.

It's basically just grabbing a region from a bitmap and storing as a frame. The math relies on the sprites being uniform.

I would definitely encourage you to understand the concepts involved, cause using code that you don't understand can lead to problems. If you understand the concept, you can apply it in any language.


I get the concept, it's not something that didn't take a little playing around with when I was using the Slick game engine. I don't know if it's standard for every sprite in the game to be on the same sprite sheet, but I simply keep a sprite sheet for each character. I.e., there's a sprite-sheet for a particular character's attack animation. Though if you understand how the spritesheet is structured it's not a difficult feat, all it takes is to play around with the coordinates a little.

This post has been edited by Deviara: 22 April 2011 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Best way to animate a sprite?

Posted 22 April 2011 - 12:09 PM

View PostDeviara, on 22 April 2011 - 11:55 AM, said:

View PostPrototypical, on 22 April 2011 - 10:41 AM, said:

Great, but the concept is pretty universal. You probably didn't take note that the spritesheet the user posted, had sprites of different dimensions -- it's imperative that the sprites are of the same dimensions.

It's basically just grabbing a region from a bitmap and storing as a frame. The math relies on the sprites being uniform.

I would definitely encourage you to understand the concepts involved, cause using code that you don't understand can lead to problems. If you understand the concept, you can apply it in any language.


I get the concept, it's not something that didn't take a little playing around with when I was using the Slick game engine. I don't know if it's standard for every sprite in the game to be on the same sprite sheet, but I simply keep a sprite sheet for each character. I.e., there's a sprite-sheet for a particular character's attack animation. Though if you understand how the spritesheet is structured it's not a difficult feat, all it takes is to play around with the coordinates a little.


Yes, for the code that was posted, you should only have character per spritesheet.

There are other ways to do a spritesheet though, where the spritesheet is accompanied by a file that has the coordinates of all the sprites on the sheet.

Zwoptex is a good example : Zwoptext

Zwoptex will allow you to load in images and it will do the dirty work of arranging them on the spritesheet, and it outputs a file with those coordinates. This allows you to load in only 1 spritesheet, instead of multiple smaller ones. Since it also finds the best way to fit them, it can sometimes save you some memory as well.

great tool

This post has been edited by Prototypical: 22 April 2011 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#9 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Re: Best way to animate a sprite?

Posted 22 April 2011 - 12:16 PM

View PostPrototypical, on 22 April 2011 - 12:09 PM, said:

View PostDeviara, on 22 April 2011 - 11:55 AM, said:

View PostPrototypical, on 22 April 2011 - 10:41 AM, said:

Great, but the concept is pretty universal. You probably didn't take note that the spritesheet the user posted, had sprites of different dimensions -- it's imperative that the sprites are of the same dimensions.

It's basically just grabbing a region from a bitmap and storing as a frame. The math relies on the sprites being uniform.

I would definitely encourage you to understand the concepts involved, cause using code that you don't understand can lead to problems. If you understand the concept, you can apply it in any language.


I get the concept, it's not something that didn't take a little playing around with when I was using the Slick game engine. I don't know if it's standard for every sprite in the game to be on the same sprite sheet, but I simply keep a sprite sheet for each character. I.e., there's a sprite-sheet for a particular character's attack animation. Though if you understand how the spritesheet is structured it's not a difficult feat, all it takes is to play around with the coordinates a little.


Yes, for the code that was posted, you should only have character per spritesheet.

There are other ways to do a spritesheet though, where the spritesheet is accompanied by a file that has the coordinates of all the sprites on the sheet.

Zwoptex is a good example : Zwoptext

Zwoptex will allow you to load in images and it will do the dirty work of arranging them on the spritesheet, and it outputs a file with those coordinates. This allows you to load in only 1 spritesheet, instead of multiple smaller ones. Since it also finds the best way to fit them, it can sometimes save you some memory as well.

great tool



That's neat, that sounds like something I worked with recently. It was an application called Tiled that allows you to generate a tilemap, which in it's simplest form is just an xml file pointing to an image resource somewhere and represents, well, a map of tiles that the user sees on the screen. The cool thing about this is that you can set additional properties for each tile, i.e. I could set a blocked property with a value of true and in code I can load up that tile map as a 2D array of individual tiles and evaluate it's blocked property when I'm doing collision detection really awesome tool.

Anyways, I digress, thanks for the tips fellas.

This post has been edited by Deviara: 22 April 2011 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1