<< Chapter < Page Chapter >> Page >

Run the program

I encourage you to copy the code from Listing 8 . Compile the code and execute it,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

In this module, you learned how to use objects of the SpriteSheet class and the Animation class to perform simple sprite sheet animation.

What's next?

In the next module, you will learn how to use objects of the SpriteSheet class and the Animation class to perform more complex sprite sheet animations than was the case in this module.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Slick0180: Sprite sheet animation, part 1
  • File: Slick0180.htm
  • Published: 02/05/13
  • Revised: 06/08/15 for 64-bit
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

A complete listing of the program discussed in this module is provided in Listing 8 .

Listing 8 . Source code for Slick0180 .
/*Slick0180.java Copyright 2013, R.G.BaldwinUses one row of sprites from a sprite sheet along with an Animation object to draw an animation of a dog playing.By default, the program displays one cycle of five sprites per second. Clock time is displayed below theanimation. The time that each image of the dog is displayed isindependent of the frame rate. Demonstrate this by changing the value of targetDelta and observing therelationship between the animation times and the clock. For best results, keep the targetDelta less than thedisplay time for each sprite (duration). Tested using JDK 1.7 under WinXP*********************************************************/ import org.newdawn.slick.AppGameContainer;import org.newdawn.slick.BasicGame; import org.newdawn.slick.GameContainer;import org.newdawn.slick.Graphics; import org.newdawn.slick.Image;import org.newdawn.slick.SlickException; import org.newdawn.slick.SpriteSheet;import org.newdawn.slick.Animation; import org.newdawn.slick.Color;public class Slick0180 extends BasicGame{ Image spriteSheetImage = null;float spriteSheetWidth; float spriteSheetHeight;int spritesPerRow = 5; int spritesPerColumn = 2;int targetDelta = 16;//msec int duration = 200;//time to display each spritelong totalTime = 0;//accumulate total time for display SpriteSheet spriteSheet;Animation animation; int spriteWidth;int spriteHeight; float spriteX = 0;//sprite drawing locationfloat spriteY = 0; //----------------------------------------------------//public Slick0180(){ //Call to superclass constructor is required.super("Slick0180, Baldwin."); }//end constructor//----------------------------------------------------// public static void main(String[]args) throws SlickException{AppGameContainer app = new AppGameContainer( new Slick0180(),450,120,false);app.start();//this statement is required }//end main//----------------------------------------------------// @Overridepublic void init(GameContainer gc) throwsSlickException { spriteSheetImage = new Image("Slick0180a1.png");//Enlarge the sprite sheet. Image temp = spriteSheetImage.getScaledCopy(580,224);spriteSheetImage = temp; spriteSheetWidth = spriteSheetImage.getWidth();spriteSheetHeight = spriteSheetImage.getHeight(); System.out.println("spriteSheetWidth: " + spriteSheetWidth); System.out.println("spriteSheetHeight: " + spriteSheetHeight); spriteWidth = (int)(spriteSheetWidth/spritesPerRow);spriteHeight = (int)(spriteSheetHeight/spritesPerColumn);//Instantiate a new spriteSheet object based on the // width and height of the tiles.spriteSheet = new SpriteSheet(spriteSheetImage, spriteWidth,spriteHeight); //Create a new animation based on a selection of// sprites from the sprite sheet. animation = new Animation(spriteSheet,0,//first column 0,//first row4,//last column 0,//last rowtrue,//horizontal duration,//display timetrue//autoupdate );gc.setShowFPS(true);//show FPS ////set frame rategc.setTargetFrameRate((int)(1000/targetDelta)); //set drawing locationspriteX = spriteWidth; spriteY = 0;}//end init //----------------------------------------------------//@Override public void update(GameContainer gc, int delta)throws SlickException{ //Note that the following is for clock time display// only. It does not effect the animation. totalTime += delta;//update total time accumulator}//end update //----------------------------------------------------//public void render(GameContainer gc, Graphics g) throws SlickException{g.setDrawMode(g.MODE_NORMAL); g.setBackground(Color.gray);//Draw the currently selected animation image at the // specified locationanimation.draw(spriteX,spriteY); g.drawString("totalTime = "+totalTime/1000,10f,100f);}//end render }//end class Slick0180//======================================================//

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Anatomy of a game engine. OpenStax CNX. Feb 07, 2013 Download for free at https://legacy.cnx.org/content/col11489/1.13
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?

Ask