Android development _ libgdx game engine tutorial-beautiful custom progress bar (8)

Source: Internet
Author: User
Tags clear screen libgdx
In this article, we need to first master some simple libgdx knowledge to make it easier: Use of the android game development framework libgdx (4)-stage and actorsHttp://www.apkbus.com/android-19750-1-1.html Libgdx learning notes 2 draw imagesBytes.

  1. Public class progressbar extends Actor implements disposable {
  2. Texture platform;
  3. Texture bar;
  4. Int height;
  5. Int width;
  6. Float progress;
  7. // A simple adaptation is made. powerx and powery are the weights of the current device resolution respectively, based on the current mainstream 800*480
  8. Float powerx;
  9. Float powery;
  10. @ Override
  11. Public void draw (spritebatch batch, float arg1 ){
  12. // Todo auto-generated method stub
  13. Batch. draw (platform, (GDX. graphics. getwidth ()-bar. getwidth () * powerx)/2, 0, platform. getwidth () * powerx, platform. getheight () * powery );
  14. Batch. draw (Bar, (GDX. graphics. getwidth ()-bar. getwidth () * powerx)/2, 0, bar. getwidth () * progress/100f * powerx, bar. getheight () * powery );
  15. }
  16. @ Override
  17. Public actor hit (float arg0, float arg1 ){
  18. // Todo auto-generated method stub
  19. Return NULL;
  20. }
  21. Public progressbar (int x, int y ){
  22. Super ();
  23. // Set the location of the actor. It is useless here to introduce it to you.
  24. This. x = X;
  25. This. Y = y;
  26. Platform = new texture (GDX. Files. Internal ("black.png "));
  27. Bar = new texture (GDX. Files. Internal ("green.png "));
  28. Height = GDX. Graphics. getheight ();
  29. Width = GDX. Graphics. getwidth ();
  30. // A simple adaptation is made. powerx and powery are the weights of the current device resolution respectively, based on the current mainstream 800*480
  31. Powerx = GDX. Graphics. getwidth ()/800f;
  32. Powery = GDX. Graphics. getheight ()/480f;
  33. }
  34. Public void setprogress (float progress ){
  35. This. Progress = progress;
  36. }
  37. Public void dispose (){
  38. // Todo auto-generated method stub
  39. Platform. Dispose ();
  40. Bar. Dispose ();
  41. }
  42. }

Copy codeThe preceding variables and statements are described as follows: texture platform; texture bar; the former is the base image of the progress bar, while the bar represents the picture of the progress bar. As for the draw () method in actor, This is a method automatically called by the system. It describes how the actor instance is drawn. The draw () method does not need to be manually called, instead, after the actor is added to the stage, the stage is continuously called in the render () function of applicationlistener. draw (), the system will automatically call the draw () method of the actor that has been added to the stage, and draw the actor together. For details, see:Use of the android game development framework libgdx (4)-stage and actorsHttp://www.apkbus.com/android-19750-1-1.html

The remaining statements are also very simple. In draw (), two diagrams are drawn using spritebatch. The bar length varies according to the Progress progress, which produces the progress bar effect.

This time, we rewrite an applicationlistener and start it with androidapplication. The source code is modified and added based on the third lecture.

  1. Public class progress implements applicationlistener {
  2. Progressbar bar;
  3. Stage stage;
  4. @ Override
  5. Public void create (){
  6. // Todo auto-generated method stub
  7. Bar = new progressbar (0, 0 );
  8. // Create a stage
  9. Stage = new stage (GDX. Graphics. getwidth (), GDX. Graphics. getheight (), true );
  10. Stage. addactor (bar );
  11. }
  12. @ Override
  13. Public void dispose (){
  14. // Todo auto-generated method stub
  15. Bar. Dispose ();
  16. }
  17. @ Override
  18. Public void pause (){
  19. // Todo auto-generated method stub
  20. }
  21. @ Override
  22. Public void render (){
  23. // Todo auto-generated method stub
  24. GDX. gl. glclear (gl10.gl _ color_buffer_bit );
  25. GDX. gl. glclearcolor (1f, 1f, 1f, 0f );
  26. Stage. Act (GDX. Graphics. getdeltatime ());
  27. Stage. Draw ();
  28. If (bar. Progress <100)
  29. Bar. Progress + = 0.5;
  30. // Reset zero
  31. If (bar. Progress = 100)
  32. Bar. Progress = 0;
  33. }
  34. @ Override
  35. Public void resize (INT arg0, int arg1 ){
  36. // Todo auto-generated method stub
  37. }
  38. @ Override
  39. Public void resume (){
  40. // Todo auto-generated method stub
  41. }
  42. }

Copy codeThis is different from the previous Demo: GDX.GL. Glclear (gl10.Gl_color_buffer_bit); GDX.GL. Glclearcolor (1f, 1f, 1f, 0f); you can see that the color after the clear screen is set to (,), that is, white. You can see the effect below. Start with androidapplication:

  1. Public class mygame implements applicationlistener {
  2. Spritebatch batch;
  3. Bitmapfont BF;
  4. Particle;
  5. Particle effect TEM;
  6. Participant pool;
  7. Arraylist <participant leeffect> Participant lelist;
  8. Public void create (){
  9. // Stub
  10. Batch = new spritebatch ();
  11. BF = new bitmapfont ();
  12. // Initialize the particle variable
  13. Particle = new maid ();
  14. Particle. Load (GDX. Files. Internal ("particle. P"), GDX. Files. Internal (""));
  15. Particle pool = new maid tpool (particle, 5, 10 );
  16. Participant lelist = new arraylist <participant leeffect> ();
  17. }
  18. Public void render (){
  19. // Stub
  20. GDX. gl. glclear (gl10.gl _ color_buffer_bit );
  21. GDX. gl. glclearcolor (0f, 0f, 0f, 0f );
  22. Batch. Begin ();
  23. BF. Draw (batch, "testin mkey libgdx (3)", GDX. Graphics. getwidth () * 0.4f, GDX. Graphics. getheight ()/2 );
  24. Batch. End ();
  25. If (true ){
  26. If (GDX. Input. istouched ()){
  27. // When the distance between the touch point and the previous touch point is greater than a certain value, a new particle system is triggered, which reduces the burden on the system.
  28. TEM = maid ();
  29. TEM. setposition (GDX. Input. getx (), GDX. Graphics. getheight ()-GDX. Input. Gety ());
  30. Participant lelist. Add (TEM );
  31. }
  32. Batch. Begin ();
  33. For (INT I = 0; I <participant lelist. Size (); I ++ ){
  34. Participant lelist. Get (I). Draw (batch, GDX. Graphics. getdeltatime ());
  35. }
  36. Batch. End ();
  37. // Clear the completed Particle System
  38. Particle effect temparticle;
  39. For (INT I = 0; I <participant lelist. Size (); I ++ ){
  40. Temparticle = maid. Get (I );
  41. If (temparticle. iscomplete ()){
  42. Maid. Remove (I );
  43. }
  44. }
  45. }
  46. }
  47. Public void resize (INT width, int height ){
  48. // Stub
  49. }
  50. Public void pause (){
  51. // Stub
  52. }
  53. Public void resume (){
  54. // Stub
  55. }
  56. Public void dispose (){
  57. // Stub
  58. Batch. Dispose ();
  59. BF. Dispose ();
  60. // Never forget to release the memory
  61. Particle. Dispose ();
  62. If (TEM! = NULL)
  63. TEM. Dispose ();
  64. Particle pool. Clear ();
  65. }
  66. }

Copy code

Just run it. It's done! Appendix
Welfare diagram: Haha, the following is a big game I wrote with libgdx. I haven't fully written it yet. I can leak some spy photos first. If you like libgdx, please provide more support for my testin cup libgdx series tutorials. if many people support it and everyone wants the source code, I will consider releasing the source code for everyone to learn.

In fact, a game is a pet-developed game. It can be used for normal battles, Bluetooth battles, pet evolution, and so on. I don't know if you can see any function from the source code name? Haha, This is a waiting page for asynchronous loading written using the progress bar in this article. It is not complete yet. Spy photos !!!

 

Related Article

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.