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
- # Ifndef _ helloworld_scene_h __
- # DEFINE _ helloworld_scene_h __
- # Include "cocos2d. H"
- # Include "myactionscene. H"
- Typedef Enum ①
- {
- Kexplosion = 1
- , Kfire
- , Kfireworks
- , Kflower
- , Kgalaxy
- , Kmeteor
- , Krain
- , Ksmoke
- , Ksnow
- , Kspiral
- , Ksun
- } Actiontypes; ②
- Class helloworld: Public cocos2d: Layer
- {
- Public:
- Static cocos2d: Scene * createscene ();
- Virtual bool Init ();
- Void onclickmenu (cocos2d: ref * psender );
- Create_func (helloworld );
- };
- # 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
- # Ifndef _ myaction_scene_h __
- # DEFINE _ myaction_scene_h __
- # Include "cocos2d. H"
- # Include "helloworldscene. H"
- Class myaction: Public cocos2d: Layer
- {
- Cocos2d: labelbmfont * plabel; ①
- Public:
- Static cocos2d: Scene * createscene ();
- Virtual bool Init ();
- Virtual void onentertransitiondidfinish (); ②
- Create_func (myaction );
- Void backmenu (cocos2d: ref * psender); ③
- };
- # 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
- Bool myaction: Init ()
- {
- If (! Layer: Init ())
- {
- Return false;
- }
- Size visiblesize = Director: getinstance ()-> getvisiblesize ();
- Auto pitmlabel = label: createwithbmfont ("fonts/fnt8.fnt", "<back ");
- Auto backmenuitem = menuitemlabel: Create (pitmlabel,
- Cc_callback_1 (myaction: backmenu, this ));
- Backmenuitem-> setposition (point (visiblesize. Width-100,100 ));
- Menu * Mn = menu: Create (backmenuitem, null );
- Mn-> setposition (point: zero );
- This-> addchild (Mn );
- Plabel = label: createwithbmfont ("fonts/fnt8.fnt ","");
- Plabel-> setposition (point (visiblesize. width/2, visiblesize. Height-50 ));
- This-> addchild (plabel, 3 );
- Return true;
- }
- Void myaction: onentertransitiondidfinish ()
- {
- Layer: onentertransitiondidfinish ();
- Particle System * system;
- Switch (this-> gettag () {①
- Case kexplosion:
- System = participant explosion: Create ();
- Plabel-> setstring ("explosion ");
- Break;
- Case kfire:
- System = participant fire: Create ();
- Plabel-> setstring ("fire ");
- Break;
- Case kfireworks:
- System = MAID: Create ();
- Plabel-> setstring ("Fireworks ");
- Break;
- Case kflower:
- System = MAID: Create ();
- Plabel-> setstring ("Flower ");
- Break;
- Case kgalaxy:
- System = participant galaxy: Create ();
- Plabel-> setstring ("Galaxy ");
- Break;
- Case kmeteor:
- System = MAID: Create ();
- Plabel-> setstring ("Meteor ");
- Break;
- Case Krain:
- System = MAID: Create ();
- Plabel-> setstring ("Rain ");
- Break;
- Case ksmoke:
- System = MAID: Create ();
- Plabel-> setstring ("smoke ");
- Break;
- Case ksnow:
- System = MAID: Create ();
- Plabel-> setstring ("snow ");
- Break;
- Case kspiral:
- System = MAID: Create ();
- Plabel-> setstring ("Spiral ");
- Break;
- Case ksun:
- System = participant Sun: Create (); ②
- Plabel-> setstring ("sun ");
- Break;
- }
- Size visiblesize = Director: getinstance ()-> getvisiblesize ();
- System-> setposition (point (visiblesize. width/2, visiblesize. Height/2 ));
- 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.
For more information, please refer to the first domestic swift book "swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf
Welcome to Zhijie IOS public classroom Platform
Cocos2d-x built-in Particle System