Effect of cocos2d-x Particles
There are two methods to implement large-scale moving objects.
1. Use Frame Animation to simulate
2. Particle effect
The particle system has the CCParticleSystem class implementation, and the CCParticleSystem implements particle control and scheduling. Operations on particles include:
1. generate particles
2. Update the particle state.
3. Reclaim invalid Particles
The particle system inherits from CCNode and can be added to other nodes.
The cocos2d-x has built-in particle effects such:
-CCParticleSystem (parent class of all particle systems)
-- CCParticleSystemPoint and CCParticleSystemQuad (point particle and square particle system both inherit all attributes of CCParticleSystem)
-- CCParticleExplosion: particle Blast Effect
-- CCParticleFireworks: Fireworks particle effect
-- CCParticleFire: Flame particle effect
-- CCParticleFlower: Bouquet particle effect
-- CCParticleGalaxy: Galaxy particle effect
-- CCParticleMeteor: meteor particle effect
-- CCParticleSpiral: vortex particle effect
-- CCParticleSnow: snow particle effect)
-- CCParticleSmoke: smoke particle effect
-- CCParticleSun: Sun particle effect
-- CCParticleRain: Effect of rain Particles
Example:
Particle System * particle = FIG: create ();
Particle-> setTexture (CCTextureCache: sharedTextureCache ()-> addImage ("snow.png "));
This-> addChild (particle );
The above code is added to init to see the effect of the snowflake particles.
The first thing you need to know is the particle emitter mode. Different transmitter modes have different available parameters, which may produce different effects. This is a bit like a mold, if any model is used to generate a model product, an error may be reported if any parameter is referenced:
1. Gravity mode-> setEmitterMode (kCCParticleModeGravity)
In gravity mode, setGravity can be set based on the set gravity point, and additional parameters such as velocity, Radial Acceleration, and tangent acceleration ...... To obtain the corresponding particle effect.
2. radius mode-> setEmitterMode (kCCParticleModeRadius)
In this mode, the range and action of the particle are divided by the circle and radius, like the above center explosion effect, I set the initial activity Radius Range of the particle and the activity Radius Range at the end of the particle lifecycle.
There are also three effect modes of the particle on the motion position coordinate-> setPositionType (). Here we refer to an explanation:
Assume that particle transmitter A is added to SpriteB, (B-> addChild ())
1. kCCPositionTypeFree
When B is in motion, if Free is set, the particles emitted by A will be dragged to the end. If other parameters are set, the tail will not be dragged.
2. kCCPositionTypeRelative
When A changes the coordinates of B, if Relative is set, the particles emitted by A will be dragged to the end.
3. kCCPositionTypeGrouped
When Grouped is set, no matter what the coordinates change, the tail will not be dragged.
In summary, Free is Relative to the world, Relative is Relative to the parent node, and Grouped is Relative to the transmitter coordinate.
@ 1 -- create a Particle System Based on the plist file:
- CCParticleSystemQuad * emitter1 = CCParticleSystemQuad: create ("Particle/BurstPipe. plist ");
-
- Emitter1-> setPosition (ccp (size. width/2, size. height/2); // you can specify the position of the emission particle.
-
- Emitter1-> setAutoRemoveOnFinish (true); // After completion, the brake is removed.
-
- Emitter1-> setDuration (10); // sets the duration of the Particle System in seconds.
-
- This-> addChild (emitter1, 0, 1 );
Do not use the plist file to create the particle effect
Auto size = Director: getInstance ()-> getWinSize ();
Auto m_emitter = MAID: createwithtotalparticipant (900 );
M_emitter-> setTexture (Director: getInstance ()-> getTextureCache ()-> addImage ("snow.png "));
// The code below we can use both in 2.x and 3.x
M_emitter-> setDuration (-1 );
M_emitter-> setGravity (Point (0,-240); // in Cocos2d-x-2.x CCPoint (0,-240) is used
M_emitter-> setAngle (90 );
M_emitter-> setAngleVar (360 );
M_emitter-> setRadialAccel (50 );
M_emitter-> setRadialAccelVar (0 );
M_emitter-> setTangentialAccel (30 );
M_emitter-> setTangentialAccelVar (0 );
M_emitter-> setPosition (Point (size. width/2, size. height ));
M_emitter-> setPosVar (Point (400, 0 ));
M_emitter-> setLife (4 );
M_emitter-> setLifeVar (2 );
M_emitter-> setStartSpin (30 );
M_emitter-> setStartSpinVar (60 );
M_emitter-> setEndSpin (60 );
M_emitter-> setEndSpinVar (60 );
M_emitter-> setStartColor (Color4F (255,255,255, 1 ));
M_emitter-> setStartColorVar (Color4F (0, 0, 0 ));
M_emitter-> setEndColor (Color4F (255,255,255, 1 ));
M_emitter-> setEndColorVar (Color4F (0, 0, 0, 0 ));
M_emitter-> setStartSize (30 );
M_emitter-> setStartSizeVar (0 );
M_emitter-> setEndSize (20366f );
M_emitter-> setEndSizeVar (0 );
M_emitter-> setEmissionRate (100 );
AddChild (m_emitter, 10 );
In gravity mode, the following attributes are valid:
Gravity (Point ). The gravity of the particle system.
Speed (float ). The velocity at which particles are emitted.
SpeedVar (float) speed variable
Tangent acceleration of tangencialAccel (float) particles
Variable of the tangent acceleration of tangencialAccelVar (float) particles
Radius acceleration of radialAccel (float) particles.
RadialAccelVar (float) variable of radius acceleration of Particles
Radius mode has the following valid attributes:
StartRadius (a float). Start radius of the particle
StartRadiusVar (a float). Start radius variable
EndRadius (a float). Particle end radius
EndRadiusVar (a float). End radius variable
RotatePerSecond (a float). The rotation angle per second around a source point
RotatePerSecondVar (a float). rotation angle variable per second
General Attributes of particles:
StartSize: initial particle size (pixel value)
StartSizeVar
EndSize: The ending size of the particle (if you want the starting size of the particle to be the same as the ending laugh, use kCCParticleStartSizeEqualToEndSize
EndSizeVar
StartColor: particle start color (ccColor4F)
StartColorVar
EndColor: The ending color of the particle.
EndColorVar
StartSpin: used only in CCParticleSystemQuad. The starting degree of Helix
StartSpinVar
EndSpin: used only in CCParticleSystemQuad to end the helix.
EndSpinVar
Life particle life time, in seconds
LifeVar
Angle: particle start angle, float
AngleVar
Positon: CCPoint particle position
PositonVar
CenterOfGravity: CGPoint
Common system attributes:
EmissionRate duration (a float): number of particles emitted per second
Duration (a float): Particle System survival time
BlendFunc (a ccBlendFunc): the OpenGL rendering function used for system rendering.
PositionType (a tCCPositionType).: Use the kCCPositionTypeFree (default) attribute to move particles freely. Or use kCCPositionTypeGrouped to move the particles in a group.
Texture (a CCTexture2D). Particle texture