Zhiyi game development tutorial cocos2d-x porting edition 007

Source: Internet
Author: User

In the new chapter, Zhiyi brings knowledge about particle systems. If you don't know what the particle system is like, let's take a look.

Why Particle System

In Example 6, we use a set of pictures to achieve the explosive effect of shells. This is a common expression in game development. Its theoretical basis is animation. But if we want to simulate more real effects, such as smoke, lightning, rain, snow, and flame, we need to use another tool-particle system.

Particle System is one of the technologies used to simulate fuzzy phenomena in computer graphics. Generally, a large number of tiny particles are created to present a phenomenon. From a macro perspective, these particles are a whole and they are deterministic and controllable. From a micro perspective, these particles fight independently, and each individual maintains its own randomness.

The particle system allows your players to shine and the entire game starts in Turin.

Popular Science

In 1983, the pioneer William T. Reeves published the article particle systems: a technique for modeling a class of fuzzy objects in computer graphics. He introduced the Particle System Used in Star Trek II: The Wrath of Khan, one of the first people to master the system.

William "bill" Reeves is written on Wikipedia, and William T is signed on the paper. reeves, I think it should be the same person, the great guy of Lucas film, one of the founding employees of Pixar.

If you are interested, you can read the master'sArticleFor more than 10 pages, I have a limited level and can see very slowly. I only read the beginning.

Use Particle System

Well, this is really complicated. To avoid misleading everyone, please refer to Chapter 7th of Zhiyi's tutorial for details on Particle System.

Http://blog.sina.com.cn/s/blog_537cabb80100i0qu.html

Or the old G's Study Notes 14

Http://4137613.blog.51cto.com/4127613/767801

Let's skip the boring principle and parameter introduction and focus on the basic usage.

1) basic usage

Like other classes, the particle effect class for cocos2d-x also provides static functions for us to easily create their instances.

 1 Emitter2 = Ccparticlesun: node ();  2 Emitter2-> Retain ();  3 Emitter2-> settexture (cctexturecache: sharedtexturecache ()-> Addimage (s_fire ));  4 Emitter2-> setposvar (CCP ( 3 , 3 ));  5 Emitter2-> setstartsize ( 20366f  );  6 Emitter2-> setstartsizevar ( 2.0f  );  7 Emitter2-> setendsize ( 0.0f  );  8 Emitter2-> setendsizevar ( 0.0f  );  9 Cccolor4f endcolor2 = {0.0f , 0.0f , 0.0f , 0.1f  };  10 Emitter2-> Setendcolor (endcolor2 );  11 Emitter2-> Stopsystem ();  12 Addchild (emitter2 );

The disadvantage of using this method is that we cannot specify the number of particles because it has fixed this value in init.

 1   Class Cc_dll ccparticlesun: Public Arch_optimal_participant _system  2   {  3   Public  :  4   Ccparticlesun (){}  5       Virtual ~ Ccparticlesun (){}  6       //  The object instance created using the node method has 350 particles by default.  7       Bool Init (){Return Initwithtotalparticipant ( 350  );}  8       Virtual   Bool Initwithtotalparticipant (unsigned Int  Numberofparticipant );  9       Static Ccparticlesun * Node ()  10   {  11 Ccparticlesun * pret =New  Ccparticlesun ();  12           If (Pret-> Init ())  13   {  14 Pret-> Autorelease ();  15               Return  Pret;  16   }  17   Cc_safe_delete (pret) 18           Return  NULL;  19   }  20 };
2) more flexible applications

To customize the number of particles, we will use another method:

 1 Emitter2 = New  Ccparticlesun ();  2 Emitter2-> initwithtotalparticipant ( 250  );  3 Emitter2-> settexture (cctexturecache: sharedtexturecache ()->Addimage (s_fire ));  4 Emitter2-> setposvar (CCP ( 3 , 3  ));  5 Emitter2-> setstartsize ( 20366f  );  6 Emitter2-> setstartsizevar ( 2.0f  );  7 Emitter2-> setendsize ( 0.0f  ); 8 Emitter2-> setendsizevar ( 0.0f  );  9 Cccolor4f endcolor2 = { 0.0f , 0.0f , 0.0f , 0.1f  };  10 Emitter2-> Setendcolor (endcolor2 );  11 Emitter2-> stopsystem ();
3) method 3

Another way is to use the particle editor to create a particle effect file. I have not tried this method. I can only try it later.

Notes

Note that no matter which method is used to create a particle effect object,Make sure that the texture is set.Otherwise, the rendering will crash.

In addition, it involves managing the object lifecycle in the cocos2d-x.

If you use method 1 and save the created object in the member variable, you need to call retain to prevent it from being automatically released, and remember to call release to release it when it is not needed.

If you are using method 2, you must always remember the principle that "Whoever applies is responsible for releasing. However, for these ccobject objects, use release instead of directly Delete, because it may be more than one reference.

Summary

Although a slight change in complicated parameters may have a new effect, do not have any mental burden. You must be confident that there are only a few commonly used parameters, if you use more, you will naturally be comfortable. The usage of different effects in the particle system is the same. Even a new effect that has never been used can be immediately used as long as you learn it.

Example 7

Based on the previous example, we only made two changes to add smoke effects to enemy tank shells and flame effects to our tank shells.

They are all very simple calls. If you have any problems, you can refer to my uploadedCode.

Http://files.cnblogs.com/cocos2d-x/ZYG007.rar

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.