Cocos2d-x custom particle system, cocos2d-x Particles

Source: Internet
Author: User

Cocos2d-x custom particle system, cocos2d-x Particles
In addition to using 11 built-in particle systems for the Cocos2d-x, we can also customize particle systems by creating a particle systemquad object and setting properties, in this way, we can fully implement the various effects of the particle system. There are at least two ways to use the user-defined particle system: code creation and plist file creation. Code Creation

The so-called code creation is fully implemented through code, and all the attributes are set through the program code. This requires developers to be very familiar with these attribute values, and this method cannot be previewed. They can only view, adjust, and run the program to view the effect, and then adjust the effect. This is troublesome.

To implement the snowy particle system as shown in, we can certainly implement it through the method described above, but this section first introduces how to implement it through a custom particle system.

 

The main code of the snowy particle system created by the Code is as follows:

Bool HelloWorld: init () {if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); auto bg = Sprite: create ("background-1.png "); bg-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); this-> addChild (bg); auto maid (200); ① // set the snowflake particle texture image:: getInstance ()-> addImage ("snow.png"); ② // set the duration of the particle emission.-1 indicates that the particle is always sustained ); // set the particle's gravity direction to the particle system-> setGravity (Point (0,-240); // set the angle and deviation of the particle system-> setAngle (90 ); particle System-> setAngleVar (360); // you can specify the Radial Acceleration and the deviation. The parameter value is: setRadialAccel (50), and the parameter value is: setRadialAccelVar (0 ); // sets the tangent acceleration and deviation of the particle. The parameter value is: setTangentialAccel (30); the parameter value is: Particle System-> setTangentialAccelVar (0 ); // set the particle initialization location deviation (). // set the particle lifetime and deviation ); particle System-> setLifeVar (2); // you can specify the Rotation Angle and deviation of a particle at the start of a particle. For example, you can set the Rotation Angle and deviation of a particle system-> setStartSpin (30 ); // set the Rotation Angle and deviation at the end of the process. The value is "participant system"> setEndSpin (60); the value is "participant system"> setEndSpinVar (60 ); // set the color and deviation at the beginning of the system-> setStartColor (Color4F (,); // set the color and deviation at the end of the system-> setEndColor (Color4F, (1, 1); // set the particle size and deviation at the beginning. (optional): Particle System-> setStartSize (30); (optional): Particle System-> setStartSizeVar (0 ); // set the particle size and deviation at the particle end system-> setEndSize (20366f); Particle System-> setEndSizeVar (0 ); // set the number of particles generated per second. The value of the setEmissionRate (100) and the value of the setPosition (Point (visibleSize. width/2, visibleSize. height + 50); this-> addChild (participant system); return true ;}

The Code in line ①: createwithtotalparticipant (200) is used to create a particle systemquad object. The static createwithtotalparticipant function is used to create a particle object by specifying the initial number of particles.

The Code in line ② specifies the texture of the particle. The TextureCache: getInstance ()-> addImage ("snow.png") statement can create the texture object Texture2D through the specified texture image, texture Image width and height must be 2 to n power, and the size should not exceed 64x64 pixels. When designing a texture image, you do not need to pay attention to too many details. For example, when designing a snowflake texture image, according to the snowflake, there are six corners. Many people will design the style shown in Figure 10-7. In fact, we need the dot of the gradient effect shown in Figure 10-8.

Snowflake Image

 

Snowflake particle texture image

 

Plist File Creation

The code creation method requires many attributes to be maintained. It is very difficult to manually adjust these attributes. We recommend using Particle Designer and other Particle design tools for WYSIWYG design, these tools generate a plist file describing the attributes of particles, and then load the file using statements similar to the following:

Auto particle system = MAID: create ("snow. plist ");

Snow. plist is an attribute file describing motion, and plist is an XML file. The reference code is as follows:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>angle</key><real>270</real><key>angleVariance</key><real>5</real><key>blendFuncDestination</key><integer>771</integer><key>blendFuncSource</key><integer>1</integer><key>duration</key><real>-1</real><key>emitterType</key><real>0.0</real><key>finishColorAlpha</key><real>1</real><key>finishColorBlue</key><real>1</real><key>finishColorGreen</key><real>1</real><key>finishColorRed</key><real>1</real><key>finishColorVarianceAlpha</key><real>0.0</real><key>finishColorVarianceBlue</key><real>0.0</real><key>finishColorVarianceGreen</key><real>0.0</real><key>finishColorVarianceRed</key><real>0.0</real><key>finishParticleSize</key><real>-1</real><key>finishParticleSizeVariance</key><real>0.0</real><key>gravityx</key><real>0.0</real><key>gravityy</key><real>-10</real><key>maxParticles</key><real>700</real><key>maxRadius</key><real>0.0</real><key>maxRadiusVariance</key><real>0.0</real><key>minRadius</key><real>0.0</real><key>minRadiusVariance</key><real>0.0</real><key>particleLifespan</key><real>3</real><key>particleLifespanVariance</key><real>1</real><key>radialAccelVariance</key><real>0.0</real><key>radialAcceleration</key><real>1</real><key>rotatePerSecond</key><real>0.0</real><key>rotatePerSecondVariance</key><real>0.0</real><key>rotationEnd</key><real>0.0</real><key>rotationEndVariance</key><real>0.0</real><key>rotationStart</key><real>0.0</real><key>rotationStartVariance</key><real>0.0</real><key>sourcePositionVariancex</key><real>1200</real><key>sourcePositionVariancey</key><real>0.0</real><key>speed</key><real>130</real><key>speedVariance</key><real>30</real><key>startColorAlpha</key><real>1</real><key>startColorBlue</key><real>1</real><key>startColorGreen</key><real>1</real><key>startColorRed</key><real>1</real><key>startColorVarianceAlpha</key><real>0.0</real><key>startColorVarianceBlue</key><real>0.0</real><key>startColorVarianceGreen</key><real>0.0</real><key>startColorVarianceRed</key><real>0.0</real><key>startParticleSize</key><real>10</real><key>startParticleSizeVariance</key><real>5</real><key>tangentialAccelVariance</key><real>0.0</real><key>tangentialAcceleration</key><real>1</real><key>textureFileName</key><string>snow.png</string></dict></plist>

The properties and attribute values described in the preceding plist file are displayed in pairs. The <key> label describes the attributes and the <real> attribute values. The plist file describes the attributes of particles and requires a particle texture image. The textureFileName attribute in the plist file specifies the texture image, we need to place plist files and texture images under the Resources directory.

The plist file indicating particle properties can be generated using the particle system design tool. For details about how to use the particle system tool, refer to my TODO.

 

The main code is as follows:

bool HelloWorld::init(){if ( !Layer::init() ){return false;} Size visibleSize = Director::getInstance()->getVisibleSize(); auto bg = Sprite::create("background-1.png");bg->setPosition(Point(visibleSize.width/2, visibleSize.height /2));this->addChild(bg); auto particleSystem = ParticleSystemQuad::create("snow.plist");    particleSystem->setPosition(Point(visibleSize.width/2, visibleSize.height - 50));this->addChild(particleSystem);return true;}

From the code, the plist file is much easier to create a particle system than the code, mainly because plist is used to describe particle attributes.




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


 


How to customize a function in an existing class in a cocos2d-x?

Class CustomBaseSprite: public Sprite {public: void f ();};
 
Cocos2dx225 A cclayer customized in the project created using python prompts compilation error in the include header file in AppDelegatecpp

The environment is not configured, and the source code of the cocos framework is not referenced. For more information about the environment tutorial on the internet, see the missing steps.
 

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.