开发者

How can I animate a sequence of pictures in Java?

Ok, im working a little game, and I have an attack function that handles the actual attacking part for the character, but I can't figure out how make the three pictures that comprise the attack motion (one with sword up, then diagonal, then directly in front) animate so it actually looks like the sword is swinging.

Also, how do i make it so that when the character walks offs开发者_StackOverflow社区creen the view changes to display the new view. Like i walk one column past the screen and it displays a new column and the first column in the screen is now offscreen?


I'd follow this tutorial: http://www.tonypa.pri.ee/tbw/start.html

It's not for Java, but you can adapt the code and concepts to your needs. The algorithms are what are important.

For animating characters, I'd create a Sprite class which stores a set of images. When it comes to drawing, you just call a paint(..) method and pass in the Graphics2D object. In here, update the current image in the paint(..) method depending on the time elapsed since the last call (use System.currentTimeMillis()), and then draw that using the Graphics2D object. If you're especially clever, you can do an algorithm to work out what frame the sprite is in without storing any state information (using modulo and a specified common time denominator, say 2ms -- making sure that frame times are multiples of 2ms).

If you're looking for a game development library, there's a pretty good one that recently came out -- the Shaven Puppy Game Library (spgl) that uses LWJGL (Lightweight Java Game Library - uses SDL / OpenGL) made by the creators of Revenge of the Titans, a successful 2D game coded in Java using hardware acceleration. It has a huge sprite framework. Probably the biggest I've ever seen. You can define sprites in a text file, along with the sounds that the sprite makes. Another is Slick. Simple ones are GTGE and JGame.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜