Android development _ libgdx game engine tutorial applicationlistener, start the first program! (6)

Source: Internet
Author: User
Tags libgdx
In order to initialize the application, you must implement the applicationlistener interface, which is used to update the game status (that is, logic), render the items, pause the game, save the status, and release resources. It also has a place to handle application declaration periodic events. Each application, regardless of the background platform version, must implement the applicationlistener interface. All platforms will have and implement this interface.

A simple implementation is as follows:

  1. Public class mygame implements applicationlistener {
  2. Public void create (){
  3. // Stub
  4. }
  5. Public void render (){
  6. // Stub
  7. }
  8. Public void resize (INT width, int height ){
  9. // Stub
  10. }
  11. Public void pause (){
  12. // Stub
  13. }
  14. Public void resume (){
  15. // Stub
  16. }
  17. Public void dispose (){
  18. // Stub
  19. }

Copy code


I will draw a simple figure to show the framework we have written according to the steps described at the end of the first lecture.

It is easy to see that andriodapplication (inherited from activity as I mentioned earlier) plays a role in an activity, however, we do not do much work in this "activity" (of course we can do some work to save the context and so on ), the actual display work is implemented by a class that implements the applicationlistener interface. It has all the functions we need: creation, rendering, and so on. So the actual situation is that most of our actual drawing work in the game is done on this page. Next we will print some marks on the screen on the basis of the previous lecture! (Today's source code will be included at the end of the Post. You can download it !) For more information about this part, see the Administrator's post http://www.apkbus.com/forum.php? MoD = viewthread & tid = 5345 & reltid = 44505 & pre_thread_id = 0 & pre_pos = 3 & ext = print a line first:
"Testin-mkey libgdx (2 )"

  1. Public class mygame implements applicationlistener {
  2. Spritebatch batch;
  3. Bitmapfont BF;
  4. Public void create (){
  5. // Stub
  6. Batch = new spritebatch ();
  7. BF = new bitmapfont ();
  8. }
  9. Public void render (){
  10. // Stub
  11. GDX. gl. glclear (gl10.gl _ color_buffer_bit );
  12. GDX. gl. glclearcolor (0f, 0f, 0f, 0f );
  13. Batch. Begin ();
  14. BF. Draw (batch, "testin -- mkey libgdx (2)", GDX. Graphics. getwidth ()/2, GDX. Graphics. getheight ()/2 );
  15. Batch. End ();
  16. }
  17. Public void resize (INT width, int height ){
  18. // Stub
  19. }
  20. Public void pause (){
  21. // Stub
  22. }
  23. Public void resume (){
  24. // Stub
  25. }
  26. Public void dispose (){
  27. // Stub
  28. Batch. Dispose ();
  29. BF. Dispose ();
  30. }
  31. }

Copy codeFirst, remember to initialize the corresponding variables in the CREATE () function. Otherwise, a null pointer will be obtained. Spritebatch and bitmapfont are used for plotting. As for the latter, we can clearly see from the name that its implementation uses images to display fonts, so what exactly is it? We will discuss this in later tutorials and give you a detailed answer.

It is also very important that spritebatch must call the in () method before drawing, and the end () method must be called after calling, otherwise the program will report an error. The reason is also obvious.Once applicationlistener starts running, the render function will be executed continuously, and the statements in it will also be executed continuously.GDX.GL. Glclear (gl10.Gl_color_buffer_bit); GDX.GL. Glclearcolor (0f, 0f, 0f, 0f );

These two statements provide a simple explanation. Anyone familiar with OpenGL can see that this is a clear screen function that clears the color cache and sets the refreshed screen color to (0, 0, 0 ), that is, black. If (,), it is white.

There is another question, GDX.Graphics. Getwidth ()/2. In fact, it is very simple. This is some classes provided by GDX to obtain system information. Many GDX usages will be mentioned soon.

In addition, do not forget to execute the dispose () method on the variable in the dispose () function. In libgdx, many resources must be manually released; otherwise, they will always occupy the memory, an OOM (outofmemory) error may occur. The libgdx has a structure disposable. Generally, classes that implement this interface need to be manually recycled. For details, refer to the official documentation, we found many common libgdx classes: assetmanager, bitmapfont, bitmapfontcache,

Cameragroupstrategy,
Decalbatch,
Etc1.etc1data,
Framebuffer,
Gdx2dpixmap,
Indexarray,
Indexbufferobject, indexbufferobjectsubdata, md5renderer, mesh,

Particle effect,
Participant pool tpool. pooledeffect,
Pixmap,
Pixmappacker,
Shaderprogram,
Simpletileatlas,
Skin,
Spritebatch, spritecache, stage, texture,

Textureatlas,
Tileatlas,
Tilemaprenderer,
Vertexarray,
Vertexbufferobject,
Vertexbufferobjectsubdata,
World needs to be recycled manually, so do not forget to recycle it manually. The memory of Android devices is very limited! Let's run it:


(There is a "-" in it that is not displayed. This is a problem with bitmapfont and will be discussed in the future)

I don't know if you have found a problem, that is, our program currently only has one page. Do we do all our drawing work on one page? Or should we use the API intent to switch between pages? Of course this is not the case. In the next lecture, that is, the third lecture, let's take a look at how to compile an application for multiple screens or game containers, of course we do not use the intent of the API. The origin of the screen coordinates is calculated from the lower left corner. This is not the same as the normal situation. Please note that.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.