One, Applicationlistener and Applicationadapter
Referring to the game life cycle of LIBGDX, you have to mention a very important interface Applicationlistener, which is located in the COM.BADLOGIC.GDX package, and the main responsibility of Applicationlistener is to create and display windows. As a function in Applicationlistener:
Since Applicationlistener is an interface, then it must be to implement to use, but every use to achieve so many methods, it seems a bit thankless, and good may not be used. Look back to the previously created project Applicationlistener already has an implementation of the class Applicationadapter:
Second, life cycle diagram
See these functions in the Applicationlistener, although there is a sentence explanation, but I think the official life cycle chart to see more clearly, the following is the official life cycle diagram:
Third, the program to see the life cycle
To get a clear picture of the game life cycle, let's use log to print the following log:
1, override the parent class Applicationadapter method in the main class in the core:
PackageCom.stephen.game;ImportCom.badlogic.gdx.ApplicationAdapter;ImportCom.badlogic.gdx.Gdx;Importcom.badlogic.gdx.graphics.GL20;Importcom.badlogic.gdx.graphics.Texture;ImportCom.badlogic.gdx.graphics.g2d.SpriteBatch; Public classMygdxgameextendsapplicationadapter {spritebatch batch; Texture img; @Override Public voidCreate () {batch=NewSpriteBatch (); IMG=NewTexture ("Badlogic.jpg"); } @Override Public voidrender () {Gdx.gl.glClearColor (1, 0, 0, 1); Gdx.gl.glClear (Gl20.gl_color_buffer_bit); Batch.begin (); Batch.draw (IMG,0, 0); Batch.end (); } @Override Public voidResizeintWidthintheight) { //TODO auto-generated Method Stub Super. Resize (width, height); } @Override Public voidpause () {//TODO auto-generated Method Stub Super. Pause (); } @Override Public voidResume () {//TODO auto-generated Method Stub Super. Resume (); } @Override Public voidDispose () {//TODO auto-generated Method Stub Super. Dispose (); }}
2. Add the statement for the print log:
PackageCom.stephen.game;ImportCom.badlogic.gdx.ApplicationAdapter;ImportCom.badlogic.gdx.Gdx;Importcom.badlogic.gdx.graphics.GL20;Importcom.badlogic.gdx.graphics.Texture;ImportCom.badlogic.gdx.graphics.g2d.SpriteBatch; Public classMygdxgameextendsapplicationadapter {spritebatch batch; Texture img; PrivateString tag = "Tag"; Private BooleanIsrender =false; @Override Public voidCreate () {Gdx.app.log (tag,"Create"); Batch=NewSpriteBatch (); IMG=NewTexture ("Badlogic.jpg"); } @Override Public voidrender () {if(!Isrender) { //because render will continue to execute here, a flag is added.Gdx.app.log (Tag, "Render"); Isrender=true; } Gdx.gl.glClearColor (1, 0, 0, 1); Gdx.gl.glClear (Gl20.gl_color_buffer_bit); Batch.begin (); Batch.draw (IMG,0, 0); Batch.end (); } @Override Public voidResizeintWidthintheight) { //TODO auto-generated Method Stub Super. Resize (width, height); Gdx.app.log (Tag,"Resize"); } @Override Public voidpause () {//TODO auto-generated Method Stub Super. Pause (); Gdx.app.log (Tag,"Pause"); } @Override Public voidResume () {//TODO auto-generated Method Stub Super. Resume (); Gdx.app.log (Tag,"Resume"); } @Override Public voidDispose () {//TODO auto-generated Method Stub Super. Dispose (); Gdx.app.log (Tag,"Dispose"); }}
3, run the program, view the log:
(1) When the game starts:
(2) Press the HOME key:
(3) Press the Home button to click on the game icon:
(4) Press the back key:
(5) Return to the game again:
(6) Close the game directly from the Mission management office:
Four, summary
The description of the official API above, the official life cycle chart and the logs printed by our program can be summarized as follows:
Create (): Called when the game is first created, and in this function you can initialize some of our own information.
Resize (): Called when the Create method finishes executing, and also when the screen size changes when the game is active.
Render (): This function starts running after resize executes, and is constantly running the render game image interface.
Pause (): Called when the game enters the background, the game is inactive, but the game still exists.
Resume (): Called when the game is inactive from active to active state.
Dispose (): Called when the game is destroyed.
The life cycle of the LIBGDX game