<< Chapter < Page Chapter >> Page >
Listing 1 . Beginning of the class named Slick0190.
public class Slick0190 extends BasicGame{ Image spriteSheetImage = null;float spriteSheetWidth; float spriteSheetHeight;int spritesPerRow = 5; int spritesPerColumn = 2;int spriteWidth; int spriteHeight;int targetDelta = 16;//msec SpriteSheet spriteSheet;Animation animation = new Animation(); //Horizontal and vertical drawing coordinates.float spriteX = 0; float spriteY = 0;//----------------------------------------------------// public Slick0190(){//Call to superclass constructor is required. super("Slick0190, Baldwin.");}//end constructor //----------------------------------------------------//public static void main(String[] args)throws SlickException{ AppGameContainer app = new AppGameContainer(new Slick0190(),450,120,false); app.start();//this statement is required}//end main

The init method

Most of the new code in this program is contained in the init method, which begins in Listing 2 . However, the code in Listing 2 is not new and should not require an explanation beyond the embedded comments.

Listing 2 . Beginning of the init method.
public void init(GameContainer gc) throws SlickException {//Create a SpriteSheet object spriteSheetImage = new Image("Slick0190a1.png");//Enlarge the sprite sheet. Image temp = spriteSheetImage.getScaledCopy(580,224);spriteSheetImage = temp; spriteSheetWidth = spriteSheetImage.getWidth();spriteSheetHeight = spriteSheetImage.getHeight(); spriteWidth = (int)(spriteSheetWidth/spritesPerRow);spriteHeight = (int)(spriteSheetHeight/spritesPerColumn);//Instantiate a new spriteSheet object based on the // width and height of the individual tiles on the// sheet. spriteSheet = new SpriteSheet(spriteSheetImage,spriteWidth, spriteHeight);

Begin populating the Animation object.

The code in Listing 3 begins the process of populating the Animation object using images extracted from the sprite sheetshown in Figure 1 .

Listing 3 . Begin populating the Animation object.
//Populate the Animation object //Begin by adding four sets of five sprites from the// top row with the images flipped to face right. for(int cntr = 0;cntr<4;cntr++){ for(int cnt = 0;cnt<5;cnt++){ animation.addFrame(spriteSheet.getSprite(cnt,0).getFlippedCopy( true,false),100);}//end inner loop }//end outer loop

The inner loop in Listing 3 calls the getSprite method of the SpriteSheet five times in succession to extract each of the images in the top row in Figure 1 .

Flip the images

The sprites represented by those five images are facing the wrong direction.Therefore, Listing 3 calls the getFlippedCopy method of the Image class to flip the images horizontally before adding them to the contents of the Animation object.

The addFrame method

The version of the addFrame method used in Listing 3 requires two parameters:

  • A reference to an Image object
  • The time duration in milliseconds that the image should be displayed in the ongoing animation process

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