<< Chapter < Page | Chapter >> Page > |
What is delta?
An int parameter named delta is received by the update method shown in Listing 6 . (The update method s a concrete version of the method having the same signature that is declared in the Game interface.)
According to the documentation for the update method in the Game interface, delta is
"The amount of time that has passed since last update in milliseconds"
Having that time available can be valuable in some game programs. For example, you might like for one of the actors to light a fuse on a bomb and havethat bomb detonate some given number of milliseconds later. In that case, the program would need real time information to know when to detonate the bomb.
Listing 5 shows the source code for the getDelta method.
Listing 5 . The getDelta method of the GameContainer class. |
---|
protected int getDelta() {
long time = getTime();int delta = (int) (time - lastFrame);
lastFrame = time;return delta;}//end getDelta method |
Without getting into the details, the method named getTime that is called in Listing 5 returns the amount of time, (with a resolution of one millisecond) , that has elapsed since a historical point in time before the game started running.
The GameContainer class contains a protected instance variable of type long named lastFrame that is used to store a time value.
The code in Listing 5
The difference between the two time values represents a time interval and that difference is returned as type int .
Various methods in the AppGameContainer and GameContainer classes call the getDelta method in such a way that the value of delta represents the time required to update and renderone frame when the program is running. (There are some other options as well that I may discuss in a future module.)
When the method named update is called in Listing 6 , the incoming parameter named delta contains the number of milliseconds that have elapsed since the last time that the update method was called.
When the method named getDelta is called in Listing 2 , the return value is discarded. This suggests that the call to the getDelta method in Listing 2 is made simply to cause the variable named lastFrame to be initialized with time that the start method was called.
That leaves us with one more method call from Listing 2 that we need to examine -- gameLoop . I anticipate that will be a fairly long discussion, so I am going to defer that discussion until the nextmodule.
As explained earlier, the skeleton code in Listing 6 is different from the skeleton code that I presented in earlier modules.Therefore, I encourage you to copy the code from Listing 6 . 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.
The main purpose of this module was to analyze the behavior of the Slick2D gameengine when you call the start method to cause a Slick2D game program to start running.
Notification Switch
Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?