This page is optimized for mobile devices, if you would prefer the desktop version just click here

0.9 Slick0190: sprite sheet animation, part 2  (Page 4/7)

The display time duration

The time duration was set to 100 milliseconds for each of the images from the top row of Figure 1 . However, that is not a requirement. You can set a different time duration for every image that you add to an Animation object if that is required to meet your needs.

Repeat the process

The outer loop in Listing 3 causes the process to be repeated four times. Therefore, when the code in Listing 3 finishes executing, the Animation object contains 20 images made up of four set of the five images in the top row of Figure 1 . These 20 images will be used to cause the sprite to run and jump from left to rightacross the game window.

Add images from the bottom row of Figure 1

Listing 4 uses essentially the same logic (broken into two nested loops) to add four sets of images from the bottom row of Figure 1 .

Listing 4 . Add images from the bottom row of Figure 1.
//Add two sets of five sprites from the bottom row // with the images flipped to face right.for(int cntr = 0;cntr<2;cntr++){ for(int cnt = 0;cnt<5;cnt++){ animation.addFrame(spriteSheet.getSprite(cnt,1).getFlippedCopy( true,false),400);}//end inner loop }//end outer loop//Add two sets of five sprites from the bottom row // with the images facing left.for(int cntr = 0;cntr<2;cntr++){ for(int cnt = 0;cnt<5;cnt++){ animation.addFrame(spriteSheet.getSprite(cnt,1),400); }//end inner loop}//end outer loop

Flip to face the right

The first two set of images are flipped to face to the right. The last two sets of images are not flipped. This is the reason for breaking thisprocess into a pair of nested loops instead of using a single nested loop.

If you watch the animation carefully, you will see that the sprite begins answering nature's call facing to the right. Half way through answering nature'scall, the sprite spins around and faces to the left. After that, it runs across the screen from right to left.

The display time duration

Note that the specified time duration for these twenty images is 400 milliseconds. Two major changes occur during this part of the animation (relative to the previous part) :

  • The sprite does not move horizontally while these 20 images are being displayed
  • Each of the 20 images is displayed four times as long as when the sprite is running.

Finish populating the animation object

Listing 5 finishes populating the Animation object by adding four more sets of the five images in the top row of Figure 1 . In this case, however, the images are not flipped. Therefore, they are used to cause the sprite to run from right to left across the game window.

Listing 5 . Finish populating the animation object.
//Add four sets of five sprites from the top row with // the images facing leftfor(int cntr = 0;cntr<4;cntr++){ for(int cnt = 0;cnt<5;cnt++){ animation.addFrame(spriteSheet.getSprite(cnt,0),100); }//end for loop}//end for loop gc.setShowFPS(true);//display FPS//Set frame rate gc.setTargetFrameRate((int)(1000/targetDelta));}//end init
<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.