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:
- Public class mygame implements applicationlistener {
- Spritebatch batch;
- Bitmapfont BF;
- Particle;
- Particle effect TEM;
- Participant pool;
- Arraylist <participant leeffect> Participant lelist;
- Public void create (){
- // Stub
- Batch = new spritebatch ();
- BF = new bitmapfont ();
- // Initialize the particle variable
- Particle = new maid ();
- Particle. Load (GDX. Files. Internal ("particle. P"), GDX. Files. Internal ("participant .png "));
- Particle pool = new maid tpool (particle, 5, 10 );
- }
- Public void render (){
- // Stub
- GDX. gl. glclear (gl10.gl _ color_buffer_bit );
- GDX. gl. glclearcolor (0f, 0f, 0f, 0f );
- Batch. Begin ();
- BF. Draw (batch, "testin mkey libgdx (2)", GDX. Graphics. getwidth ()/2, GDX. Graphics. getheight ()/2 );
- Batch. End ();
- }
- Public void resize (INT width, int height ){
- // Stub
- }
- Public void pause (){
- // Stub
- }
- Public void resume (){
- // Stub
- }
- Public void dispose (){
- // Stub
- Batch. Dispose ();
- BF. Dispose ();
- // Never forget to release the memory
- Particle. Dispose ();
- If (TEM! = NULL)
- TEM. Dispose ();
- Particle pool. Clear ();
- }
- }
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:
- Particle. Load (GDX. Files. Internal ("particle. P "),
- 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.
- 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:
- Public class mygame implements applicationlistener {
- Spritebatch batch;
- Bitmapfont BF;
- Particle;
- Particle effect TEM;
- Participant pool;
- Arraylist <participant leeffect> Participant lelist;
- Public void create (){
- // Stub
- Batch = new spritebatch ();
- BF = new bitmapfont ();
- // Initialize the particle variable
- Particle = new maid ();
- Particle. Load (GDX. Files. Internal ("particle. P"), GDX. Files. Internal (""));
- Particle pool = new maid tpool (particle, 5, 10 );
- Participant lelist = new arraylist <participant leeffect> ();
- }
- Public void render (){
- // Stub
- GDX. gl. glclear (gl10.gl _ color_buffer_bit );
- GDX. gl. glclearcolor (0f, 0f, 0f, 0f );
- Batch. Begin ();
- BF. Draw (batch, "testin mkey libgdx (3)", GDX. Graphics. getwidth () * 0.4f, GDX. Graphics. getheight ()/2 );
- Batch. End ();
- If (true ){
- If (GDX. Input. istouched ()){
- // 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.
- TEM = maid ();
- TEM. setposition (GDX. Input. getx (), GDX. Graphics. getheight ()-GDX. Input. Gety ());
- Participant lelist. Add (TEM );
- }
- Batch. Begin ();
- For (INT I = 0; I <participant lelist. Size (); I ++ ){
- Participant lelist. Get (I). Draw (batch, GDX. Graphics. getdeltatime ());
- }
- Batch. End ();
- // Clear the completed Particle System
- Particle effect temparticle;
- For (INT I = 0; I <participant lelist. Size (); I ++ ){
- Temparticle = maid. Get (I );
- If (temparticle. iscomplete ()){
- Maid. Remove (I );
- }
- }
- }
- }
- Public void resize (INT width, int height ){
- // Stub
- }
- Public void pause (){
- // Stub
- }
- Public void resume (){
- // Stub
- }
- Public void dispose (){
- // Stub
- Batch. Dispose ();
- BF. Dispose ();
- // Never forget to release the memory
- Particle. Dispose ();
- If (TEM! = NULL)
- TEM. Dispose ();
- Particle pool. Clear ();
- }
- }
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.