Use of the android development _ libgdx Particle System (7)

Source: Internet
Author: User
Tags libgdx

This is the third lecture of the testin cup libgdx game engine tutorial. It mainly introduces the use of particle systems in the libgdx engine. This section describes how to implement multiple game interfaces in the libgdx engine. However, some bayou have doubts about the innovation of this tutorial, so I decided to give a detailed description of the content in the fourth lecture. Starting from the first lecture, every program is built on the basis of the previous program. Each source code can be downloaded at the bottom of the tutorial.

A game engine needs to make a perfect game. There are two categories that are essential: one is the genie class and the other is the particle system. Particle systems can make a variety of exquisite effects, such as water flow, flame, smoke, etc. A beautifully crafted particle system can even make amazing results in a false chaos.

Libgdx also provides support for particle systems, and is excited that libgdx provides a visual Particle System editor that allows us to preview while editing, "What you see is what you get", and soon you can make a good particle system effect.

Libgdx Particle System Editor-particle Editor
Particle Editor: http://libgdx.googlecode.com/svn/jws/particle-editor.jnlp

Please note that after downloading the tool, you must ensure that your computer has installed JRE, that is, the Java environment, to run the tool. For the JRE installation method, ask du Niang.

This is what I opened after downloading the latest version today:

Hey, here you can do a job first, that is, save the file and save it as particle. P. This can be used later. Here, we also need an image to form a single particle. Generally, we can use a small white circle. In my source code, we can also use any image instead, of course, the results are different. I will also add an attachment that I will do at will. the P file, that is, the particle effect file, should show a rainbow color. You can use this tool to open the file and then find out one by one based on the interpretation of the parameters below, and adjust the corresponding parameters to observe the changes, and then make some better results.

The parameters in the software are explained on the official website as follows:
Delay: When the particle system starts, how long does the transmitter wait to start launching?
Duration: the time span of the transmitter, that is, the duration of the particle effect. Note that this time is different from the particle survival time.

Count: As the name implies, the number of particles that can appear at the same time has a lower limit.
Emission: the number of particles emitted per second. This parameter carries a chart with duration in the middle of the chart, which indicates that the X axis of the chart represents the time when the transmitter was alive, this chart controls the number of particles emitted per second at each time point. The upper and lower text boxes on the left are used to control the range when the initial value is generated. The '>' character on the left is used to enter another value, for example, the ">" below, the transmitter will select the random value among the two values as the upper limit, relative. if selected, it indicates that the value in the chart is relative to the initial value. Otherwise it is an absolute value.
Life: The survival time of a particle,
Life offset: determines how much life of a particle has been used before it is displayed. This allows a particle to appear when its life is 50%.
X offset and y offset: The pixel offset at the relative center of the particle.
Spawn: the shape of the transmitter used to generate particles
Spawn width and spawn Height: width and height of the transmitter shape
Size: Particle Size
Velocity:
Angle:
Rotation:
These values are used to control the trajectory of a particle:
Wind and gravity: smoke is the pixel offset per second on the X axis and Y axis within the lifetime.
Tint: The color of the particle. It can change any number of colors during the lifetime of the particle.
Transparency: transparency of Particles

Next we will make some code preparations. What we need to implement today is to simulate the touch track of particles. The Code will be further modified based on the Code mentioned above.

In mygame. in Java, add four variables: Particle effect particle, particle effect TEM, and particle pool. The first variable is the particle instance, and the second variable is a temporary variable, here we will focus on the third volume. This is an example of a particle pool. This is a class for unified management of particle systems and is responsible for managing the generation and recovery of particle systems, you can use its obtain () method to obtain a special effect instance. Of course, you can directly create a special effect instance without using this class, but if we want to produce a large number of particles (which is often required to produce more real results), particle management is a big problem, therefore, it is a good choice to use the participant pool. The added code is as follows:

  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 ("participant .png "));
  15. Particle pool = new maid tpool (particle, 5, 10 );
  16. }
  17. Public void render (){
  18. // Stub
  19. GDX. gl. glclear (gl10.gl _ color_buffer_bit );
  20. GDX. gl. glclearcolor (0f, 0f, 0f, 0f );
  21. Batch. Begin ();
  22. BF. Draw (batch, "testin mkey libgdx (2)", GDX. Graphics. getwidth ()/2, GDX. Graphics. getheight ()/2 );
  23. Batch. End ();
  24. }
  25. Public void resize (INT width, int height ){
  26. // Stub
  27. }
  28. Public void pause (){
  29. // Stub
  30. }
  31. Public void resume (){
  32. // Stub
  33. }
  34. Public void dispose (){
  35. // Stub
  36. Batch. Dispose ();
  37. BF. Dispose ();
  38. // Never forget to release the memory
  39. Particle. Dispose ();
  40. If (TEM! = NULL)
  41. TEM. Dispose ();
  42. Particle pool. Clear ();
  43. }
  44. }

Copy codeHere is a description of GDX.Files. Internal () is located in the asset root directory of the Project. Similarly, in the data folder under asset, the format should be GDX.Files. Internal ("Data/particle. P "). in addition, we have mentioned in the previous lecture that many resources in libgdx need to be manually recycled. Both of the following classes, I .e., particle effect and Particle effect tpool, implement the disposeable interface (the new version of the particle effect tpool does not have dispose () instead of using the clear () method), remember to manually recycle the resources. In addition, before using the particle efficiency tpool, initialize:

  1. Particle. Load (GDX. Files. Internal ("particle. P "),
  2. GDX. Files. Internal (""));

Copy codeThe first parameter is the editing file generated by particle Editor (note that the extension name is also obtained by yourself and you can remember it when reading the file). The second parameter is to form an image file for a single particle.

This is very important! Please pay close attention to the issue of routing and the path between the two. For example, you should be able to understand it. If the data folder is in asset, the format should be GDX. files. internal ("Data/particle. P ") and GDX. files. internal ("Data/") that is to say, PNG images do not need to be written in the code, but the file names must be consistent before they can be correctly read.

  1. Particle pool = new maid tpool (particle, 5, 10 );

Copy codeThe first parameter is the just-initialized particle. The second parameter is used to determine how many particles are generated using the first parameter as the template in the particle pool for the system to call at any time, the third parameter is the maximum number of particles in the particle pool. Below we implement the particle touch tracking simulation function, first we need to capture the touch event. And we are faced with a very realistic problem: Where do we capture touch events? To know that libgdx and API can be different, we cannot use API to get touch events in libgdx. The GDX. Input. istouched () method is provided for the input part of the GDX package to determine whether the current screen is touched. If yes, true is returned. We put this statement in the render () method to detect it at any time. The modified mygame. Java code is as follows:

  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 codeSome code for removing completed particle systems is added, which is easy to understand.

For ease of demonstration, I saved the particle. P has made appropriate changes and selected the continuous option in the bottom of the editor, that is, continuous playback. It is also intended for you to see more clearly. Let's just look at it without any picture or truth: I drew a "bus" pattern, representing the android bus forum we love. Oh, this is another particle effect map I made at will.

Let's change the particle image to see whether the effect is very clear .... In fact, every particle is an android robot!

Resource files are stored in the asset folder of the source code. You can replace them by yourself.

Here is the source code!

Edu.nju.wsj.libgdx.rar
(3.01 MB, downloads: 1259)

Upload

Click the file name to download the attachment.
Download points: Download beans-2

Continue to support # testin cup # mkey libgdx game engine tutorial!

Testin ID: ilovemkey@gmail.com in general this platform is really very useful! I used this platform to discover some memory management problems of this application. In addition, we recommend that you view the changes in CPU and memory usage of a model throughout the execution process in the test report, so that you can lock the problem more quickly. Thanks to itestin for making the test much easier!

Particle-editor.jpg
(107.48 kb, downloads: 18)

Upload

Click the file name to download the attachment.


Pacl.png
(134.32 kb, downloads: 10)

Upload

Click the file name to download the attachment.


Screenshot_2012-07-03-19-26-34.png
(191.88 kb, downloads: 11)

Upload

Click the file name to download the attachment.


207031939.png
(30.98 kb, downloads: 9)

Upload

Click the file name to download the attachment.


20703194602.png
(37.43 kb, downloads: 11)

Upload

Click the file name to download the attachment.


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.