<< Chapter < Page Chapter >> Page >

Listing 5 also takes care of some common administrative details at the end, signaling the end of the init method.

The update method

The update method begins in Listing 6 . The primary purpose of update method in this program is to control the physical placement of each sprite when it is displayed.

Listing 6 . Beginning of the update method.
public void update(GameContainer gc, int delta) throws SlickException{int stepSize = 15;//Distance the sprite moves int frame = animation.getFrame();//animation frameint oneThird = animation.getFrameCount()/3;

The getFrameCount method

On the basis of the previous discussion, we already know that the Animation object contains a sequence of 60 images or frames. Rather than to rely on that knowledge, however, Listing 6 calls the getFrameCount method on the Animation object to determine the number frames in the object.

Three groups of sprites

That value is divided by 3 and saved in the variable named oneThird . The logic that follows is based on dividing the sprites into three equal sizegroups and processing them differently depending on whether they fall in the first, second, or third group.

Get the current frame number

Listing 6 calls the getFrame method on the Animation object to determine which frame should be displayed by this iteration of the game loop. That value is saved in the variablenamed frame .

Compute the display location

Having determined which image is to be displayed, we must then compute the horizontal position within the game window at which to display the image.

The display logic

If the current frame is within the first third, the display position assigned to the frame should make it appear that the sprite is running from left to rightacross the game window. Therefore, the horizontal display coordinate values for the sprites in this group should be proportional to the frame number from 0through 19.

If the current frame is within the second third, the horizontal display coordinate should not change. (The sprite should be stationary.) The sprites in this group are all intended to be displayed in the same location.

If the current frame is within the third group, things are a little more complicated. The horizontal display coordinate values assigned to the spritesshould make it appear that the sprite is running from right to left across the game window. Therefore, the coordinate value should be equal to the rightmostexcursion less a value that is proportional to the frame number, after adjusting the frame number to account for the 20 frames during which the sprites werestationary.

This process begins in Listing 7 .

Listing 7 . Compute display locations for first 20 frames.
if(frame<oneThird){ //Sprite is moving to the right. Compute the new// position. spriteX = frame*stepSize;

Figures in the first group

Listing 7 tests to determine if the current frame is in the first third. If so, it computes a horizontal positioncoordinate value as the product of the frame number and the stepSize in pixels, which was defined in Listing 6 .

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