<< Chapter < Page Chapter >> Page >

There is nothing new or unusual about the code in Listing 9 . Therefore, no explanation beyond the embedded comments should be needed. You might want to note the instance variables at the beginning ofthe class. They will be used in the code that I will describe later.

The update method

Unlike the previous program, which simply displayed what appeared to be static images, this program causes one of the images to change as the programruns. These changes are programmed into the update method, which is shown in Listing 10 .

Listing 10 . The update method.
public void update(GameContainer gc, int delta) throws SlickException{timeAccumulator += delta; if(timeAccumulator>= flashInterval){ //Reset accumulator and change color of spider// silhouette timeAccumulator = 0;if(silohetteColor.equals(Color.white)){ silohetteColor = Color.blue;}else{ silohetteColor = Color.white;}//end if }//end if}//end update

Switch color between white and blue

Recall that (by default) the update method is executed once during each iteration of the game loop. (Other programming options are available regarding if, when, and how many times the update method is executed during one iteration of the game loop.)

The purpose of the update method is to execute the program logic.

In this case, the program logic is simple; to switch the color of the large spider in Figure 2 between white and blue on a regular schedule.

As you will see in the render method later, the color of the large spider is determined by the value of the instance variable named silohetteColor , which is initialized to white in Listing 9 . Recall that the target frame rate is set to 60 frames per second in Listing 9 . A variable named flashInterval is initialized to 128 in Listing 9 .

Also recall that the value of the incoming parameter named delta is the number of milliseconds since the last time that the update method was called. This value was used in a significant way in theearlier module titled A first look at sprite motion, collision detection, and timing control .

The program logic

Each time the update method is called, the incoming value of delta is added to the value in a variable named timeAccumulator . When the accumulated time meets or exceeds the value of flashInterval , the color is switched from white to blue, or from blue to white, depending onits current value. Also the time accumulator is set to zero and a new white/blue cycle begins.

The color switch should occur approximately every 128 milliseconds, or about eight times per second.

The render method

Recall that the execution of program logic in the update method does not cause the player's view of the game to change. It is not untilthe render method is executed that the images on the screen change.

The render method begins in Listing 11 .

Listing 11 . Beginning of the render method.
public void render(GameContainer gc, Graphics g) throws SlickException{//set the drawing mode to honor transparent pixels g.setDrawMode(g.MODE_NORMAL);g.setBackground(Color.red);//Draw the spider. spider.draw(0f,0f);//Draw a white silhouette of the spider. spider.drawFlash(133f,0f);//Draw a blue silhouette of the spider.spider.drawFlash(266f,0f,131f,128f,Color.blue);

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