Cocos2d-x built-in Particle System

Source: Internet
Author: User
Tags addchild
From the class diagram, we can see that the Cocos2d-x has built-in 11 particles, the attributes of these particles are pre-defined, we can also modify some attributes in the program code, these attributes have been set in the instance in the previous section. Built-in Particle System

The built-in 11 particle system is described as follows:

You can call this operation to create a project. The particle blast effect belongs to the radius mode.

You can call this operation to create a project. Flame particle effect, which belongs to the gravity path mode.

You can call this operation to create a project. Fireworks particle effect, which belongs to the gravity mode.

. The particle effect is a gravity mode.

The sample code is as follows. Galaxy particle effect, which belongs to the radius mode.

You can call this operation to perform the following operations. Meteor particle effect, which belongs to the gravity mode.

You can call this operation to create a project. Vortex particle effect, which belongs to the radius mode.

The name of the object. The snow particle effect belongs to the gravity mode.

. Smoke particle effect, which belongs to the gravity mode.

. The effect of the Sun particles belongs to the gravity mode.

You can call this operation to perform operations on the object. Rain particle effect, which belongs to the gravity mode.

Each of these 11 particle systems has the following two create functions, which allow you to create particle objects.

Static participant explosion * Create ()

Static maid (INT numberofparticipant)

The numberofparticipant in the createwithtotalparticipant function is the number of particles initialized.

The attributes of these 11 kinds of particles vary depending on their emission modes.

 

 

Example: built-in Particle System

The following example shows the 11 built-in particle systems. In this example, the left figure shows an operation menu scenario. You can select the menu to go to the action scenario. In the action scenario, the effect of the selected particle system is displayed. Click the return button in the lower right corner to return to the menu scenario.

 

Next let's take a look at the specific program code. First, let's take a look at the helloworldscene. h file. Its code is as follows:

 

[HTML]View plaincopy
  1. # Ifndef _ helloworld_scene_h __
  2. # DEFINE _ helloworld_scene_h __
  3. # Include "cocos2d. H"
  4. # Include "myactionscene. H"
  5. Typedef Enum ①
  6. {
  7. Kexplosion = 1
  8. , Kfire
  9. , Kfireworks
  10. , Kflower
  11. , Kgalaxy
  12. , Kmeteor
  13. , Krain
  14. , Ksmoke
  15. , Ksnow
  16. , Kspiral
  17. , Ksun
  18. } Actiontypes; ②
  19. Class helloworld: Public cocos2d: Layer
  20. {
  21. Public:
  22. Static cocos2d: Scene * createscene ();
  23. Virtual bool Init ();
  24. Void onclickmenu (cocos2d: ref * psender );
  25. Create_func (helloworld );
  26. };
  27. # Endif/_ helloworld_scene_h __

 

 

Code ① ~ ② Define an enumeration type actiontypes. 11 constants are defined in the enumeration type actiontypes. These 11 constants correspond to 11 menu items.

Next let's take a look at the next scenario myactionscene. Its myactionscene. H code is as follows:

 

[HTML]View plaincopy
  1. # Ifndef _ myaction_scene_h __
  2. # DEFINE _ myaction_scene_h __
  3. # Include "cocos2d. H"
  4. # Include "helloworldscene. H"
  5. Class myaction: Public cocos2d: Layer
  6. {
  7. Cocos2d: labelbmfont * plabel; ①
  8. Public:
  9. Static cocos2d: Scene * createscene ();
  10. Virtual bool Init ();
  11. Virtual void onentertransitiondidfinish (); ②
  12. Create_func (myaction );
  13. Void backmenu (cocos2d: ref * psender); ③
  14. };
  15. # Endif/_ myaction_scene_h __

 

 

In the header file, line ① defines the labelbmfont member variable, which is used to display the name of the Particle System in the scene. The Code in line ② declares the onentertransitiondidfinish callback function, which overwrites the parent class function and will return it after the scenario appears. The Code in line ③ declares a callback function for the return menu.

The implementation code of myactionscene: myactionscene. CCP file. Its main code is as follows:

 

[HTML]View plaincopy
  1. Bool myaction: Init ()
  2. {
  3. If (! Layer: Init ())
  4. {
  5. Return false;
  6. }
  7. Size visiblesize = Director: getinstance ()-> getvisiblesize ();
  8. Auto pitmlabel = label: createwithbmfont ("fonts/fnt8.fnt", "<back ");
  9. Auto backmenuitem = menuitemlabel: Create (pitmlabel,
  10. Cc_callback_1 (myaction: backmenu, this ));
  11. Backmenuitem-> setposition (point (visiblesize. Width-100,100 ));
  12. Menu * Mn = menu: Create (backmenuitem, null );
  13. Mn-> setposition (point: zero );
  14. This-> addchild (Mn );
  15. Plabel = label: createwithbmfont ("fonts/fnt8.fnt ","");
  16. Plabel-> setposition (point (visiblesize. width/2, visiblesize. Height-50 ));
  17. This-> addchild (plabel, 3 );
  18. Return true;
  19. }
  20. Void myaction: onentertransitiondidfinish ()
  21. {
  22. Layer: onentertransitiondidfinish ();
  23. Particle System * system;
  24. Switch (this-> gettag () {①
  25. Case kexplosion:
  26. System = participant explosion: Create ();
  27. Plabel-> setstring ("explosion ");
  28. Break;
  29. Case kfire:
  30. System = participant fire: Create ();
  31. Plabel-> setstring ("fire ");
  32. Break;
  33. Case kfireworks:
  34. System = MAID: Create ();
  35. Plabel-> setstring ("Fireworks ");
  36. Break;
  37. Case kflower:
  38. System = MAID: Create ();
  39. Plabel-> setstring ("Flower ");
  40. Break;
  41. Case kgalaxy:
  42. System = participant galaxy: Create ();
  43. Plabel-> setstring ("Galaxy ");
  44. Break;
  45. Case kmeteor:
  46. System = MAID: Create ();
  47. Plabel-> setstring ("Meteor ");
  48. Break;
  49. Case Krain:
  50. System = MAID: Create ();
  51. Plabel-> setstring ("Rain ");
  52. Break;
  53. Case ksmoke:
  54. System = MAID: Create ();
  55. Plabel-> setstring ("smoke ");
  56. Break;
  57. Case ksnow:
  58. System = MAID: Create ();
  59. Plabel-> setstring ("snow ");
  60. Break;
  61. Case kspiral:
  62. System = MAID: Create ();
  63. Plabel-> setstring ("Spiral ");
  64. Break;
  65. Case ksun:
  66. System = participant Sun: Create (); ②
  67. Plabel-> setstring ("sun ");
  68. Break;
  69. }
  70. Size visiblesize = Director: getinstance ()-> getvisiblesize ();
  71. System-> setposition (point (visiblesize. width/2, visiblesize. Height/2 ));
  72. This-> addchild (system );

 

 

In the initialization scenario of the myaction: Init () function, create a particle system object in the myaction: onentertransitiondidfinish () function, instead of creating a particle system object in the myaction: onenter () function, this is because the scenario is not displayed when the myaction: onenter () function is called. If an explosion is created in the function, the particle system is displayed once. When the scenario is displayed, the explosion is over, and we will not see the effect.

Code ① ~ Line 2 creates 11 particle systems. Here, their default attribute values are used when creating particle systems. The plabel-> setstring ("XXX") function is used to set the content of the tag in the scenario, so that the name of the particle system is displayed after entering the scenario.

Cocos2d-x built-in Particle System

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.