简介
术语粒子系统关系到计算机图形学技术,使用大量非常小的sprite或其他图像对象来模拟特定种类的”模糊”现象,通过常规的渲染技术是很难制造的——一般来说有混乱系统、自然现象、或者活学反应引起的过程。
Point vs Quad
在Cocos2d-x早期版本中,Cocos2d-x里粒子系统有两种类型:Quad 和 Point粒子系统:
CCParticleSystemQuad有很多特性是CCParticleSystemPoint不支持的:
由于CCParticleSystemPoint不支持CCParticleBatchNode.所以,CCParticleSystemPoint已经从cocos2d-x粒子系统里移除了.
CCParticleBatchNode
创建Quad粒子系统
<ol class="linenums"><li value="1" class="L0"><a target=_blank target="_blank" href="http://t.cn/RhfSa04" style="color: rgb(106, 57, 6); text-decoration: none;"><span class="tag"><code></span><span class="pln">CCParticleSystemQuad* m_emitter = newCCParticleSystemQuad(); m_emitter = CCParticleFire::create(); </span><span class="tag"></code></span></a></li></ol>
重力和半径模式
重力模式 Gravity Mode
重力模式让粒子围绕一个中心点移近或移远.它的优点是非常动态,有组织有规则的效果.你可以通过下面这行代码设置重力模式:
<ol class="linenums"><li value="1" class="L0"><span class="tag"><code></span><span class="pln">// Gravity Mode this->m_nEmitterMode = kCCParticleModeGravity; // Gravity Mode: gravity this->modeA.gravity = ccp(0,-90); </span><span class="tag"></code></span></li></ol>
下面这些属性只在重力模式下支持:
- 重力(CGPoint)。粒子系统的重力。
- 速度(float)。粒子发射时的速度。
- 速度变量speedVar(float)。速度的变异数。
- tangencialAccel(float)。粒子的正切加速度。
- tangencialAccelVar(float)。粒子正切加速度的差异数。
- radialAccel(float)。粒子的径向加速度。
- radialAccelVar(float)。粒子径向加速度的差异数。
半径模式 Radius Mode
半径模式会使粒子以圆圈方式旋转.它也可以创造螺旋效果让粒子急速前进或后退.你可以通过下面这行代码设置半径模式:
<ol class="linenums"><li value="1" class="L0"><span class="tag"><code></span><span class="pln">// Radius Mode this->m_nEmitterMode = kCCParticleModeRadius; // Radius Mode: startRadius this->modeB.startRadius = 0; this->modeB.startRadiusVar = 0;//ccp(0,0); </span><span class="tag"></code></span></li></ol>
下面这些属性只在半径模式下支持:
- startRadius(float)。粒子开始时的半径。
- startRadiusVar(float)。粒子开始时的半径变异数。
- endRadius(float)。粒子结束时的半径。
- endRadiusVar(float)。结束时粒子的半径变异数。
- rotatePerSecond(float)。粒子围绕原点每秒旋转的度数。
- rotatePerSecondVar(float)。度数的变异数。
各模式下的通用属性
粒子通用属性:
- startSize: 开始时粒子的像素大小
- startSizeVar
- endSize: 如果你愿意的话使用kCCParticleStartSizeEqualToEndSize,这样开始大小 == 结束大小
- endSizeVar
- startColor开始颜色 (ccColor4F)
- startColorVar (ccColor4F)
- endColor结束颜色 (ccColor4F)
- startSpin开始的旋转角度。只用于CCParticleSystemQuad
- startSpinVar.只用于CCParticleSystemQuad
- endSpin结束时的旋转角度。只用于CCParticleSystemQuad
- endSpinVar。只用于CCParticleSystemQuad
- life:粒子存活的时间,单位秒
- lifeVar:
- angle:(float)。粒子发射时的角度
- angleVar
- positon:(CGPoint)
- posVar
- centerOfGravity(CGPoint)重力中心点
粒子系统通用属性:
- emissionRate (float)。每秒发射多少粒子。
- duration (float)。粒子系统可以存活多少秒(与life属性不同)。使用kCCParticleDurationInfinity为无限时间。
- blendFunc (ccBlendFunc)。系统中使用的OpenGL混合方法。
- positionType (CCPositionType)。粒子自由移动使用kCCPositionTypeFree(默认)。或者使用kCCPositionTypeGrouped,使粒子在集合中移动。
- texture纹理(CCTexture2D)。粒子使用的纹理。
示例
cocos2d-x里内置的预制粒子是可以在运行时自定义的.内置粒子列表:
- CCParticleFire: Point particle system. 使用重力模式.
- CCParticleFireworks: Point particle system. 使用重力模式.
- CCParticleSun: Point particle system. 使用重力模式.
- CCParticleGalaxy: Point particle system. 使用重力模式.
- CCParticleFlower: Point particle system. 使用重力模式.
- CCParticleMeteor: Point particle system. 使用重力模式.
- CCParticleSpiral: Point particle system. 使用重力模式.
- CCParticleExplosion: Point particle system. 使用重力模式.
- CCParticleSmoke: Point particle system. 使用重力模式.
- CCParticleSnow: Point particle system. 使用重力模式.
- CCParticleRain: Point particle system. 使用重力模式.
【二】:函数
1.特效
CCParticleExplosion //爆炸粒子特效
CCParticleFire //火焰粒子特效
CCParticleFlower //花束粒子特效
CCParticleFireworks //烟花粒子特效
CCParticleGalaxy //星系粒子特效
CCParticleMeteor //流星粒子特效
CCParticleRain //下雨粒子特效
CCParticleSmoke //烟雾粒子特效
CCParticleSnow //下雪粒子特效
CCParticleSpiral //漩涡粒子特效
CCParticleSun //太阳粒子特效
2.函数
setTexture();
//设置特效贴图。这里注意。老版本中如果不设置这项会报错退出。2.1.4中不设置可以使用。
setAutoRemoveOnFinish(bool);
//设置自动释放true为自动释放。
setPositionType()
//设置移动类型
kCCPositionTypeFree//自由模式。粒子不予发射器联系,发射后粒子走自己的轨道,可以做出焰尾。
kCCPositionTypeRelative//相对模式。粒子发射器随节点移动而移动。
kCCPositionTypeGrouped//相对模式。粒子随发射器移动而移动。
3.自定义
CCParticleSystemQuad::create();
这个函数是用来加载自定义的plist文件的。怎么自定义呢?我们使用工具“红孩儿工具箱”就能做自定义特效了。
【三】:示例
1.新建一个项目:Particledemo
2.载入一张很小的图片用来做贴图。
Particledemo.cpp
//-new-//
CCSize mysize=CCDirector::sharedDirector()->getWinSize();
CCParticleExplosion特效
创建CCParticleExplosion特效
//CCParticleSystem * p1=CCParticleExplosion::create();
设置特效贴图
//p1->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
设置自动释放
//p1->setAutoRemoveOnFinish(true);
设置移动类型
//p1->setPositionType(kCCPositionTypeGrouped);
设置位置
//p1->setPosition(ccp(mysize.width/2,mysize.height/2));
添加特效
//this->addChild(p1);
CCParticleExplosion特效
//CCParticleSystem * p2=CCParticleFire::create();
//p2->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p2->setAutoRemoveOnFinish(true);
//this->addChild(p2);
CCParticleFlower特效
//CCParticleSystem * p3=CCParticleFlower::create();
p3->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p3->setAutoRemoveOnFinish(true);
//this->addChild(p3);
CCParticleFireworks特效
//CCParticleSystem * p4=CCParticleFireworks::create();
p4->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p4->setAutoRemoveOnFinish(true);
//this->addChild(p4);
CCParticleGalaxy特效
//CCParticleSystem * p5=CCParticleGalaxy::create();
p5->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p5->setAutoRemoveOnFinish(true);
//this->addChild(p5);
CCParticleMeteor特效
//CCParticleSystem * p6=CCParticleMeteor::create();
p6->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p6->setAutoRemoveOnFinish(true);
//this->addChild(p6);
CCParticleRain特效
//CCParticleSystem * p7=CCParticleRain::create();
p7->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p7->setAutoRemoveOnFinish(true);
//this->addChild(p7);
CCParticleSmoke特效
//CCParticleSystem * p8=CCParticleSmoke::create();
p8->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p8->setAutoRemoveOnFinish(true);
//this->addChild(p8);
CCParticleSnow特效
//CCParticleSystem * p9=CCParticleSnow::create();
p9->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p9->setAutoRemoveOnFinish(true);
//this->addChild(p9);
CCParticleSpiral特效
//CCParticleSystem * p10=CCParticleSpiral::create();
p10->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p10->setAutoRemoveOnFinish(true);
//this->addChild(p10);
CCParticleSun特效
//CCParticleSystem * p11=CCParticleSun::create();
p11->setTexture(CCTextureCache::sharedTextureCache()->addImage("t.jpg"));
//p11->setAutoRemoveOnFinish(true);
//this->addChild(p11);
//自定义特效
CCParticleSystem * mypat=CCParticleSystemQuad::create("1.plist");
mypat->setPosition(ccp(mysize.width/2,mysize.height/2));
this->addChild(mypat);
//-new-//
好了,我们来看看效果吧。
CCParticleSystem* particleSystem1 = CCParticleSun::create();
particleSystem1->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
//自动释放
particleSystem1->setAutoRemoveOnFinish(true);
//设置移动类型:自由模式
particleSystem1->setPositionType(kCCPositionTypeFree);
particleSystem1->setPosition(ccp(90, 160));
addChild(particleSystem1);
CCParticleSystem* particleSyatem2 = CCParticleSun::create();
particleSyatem2->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
//设置移动类型:相对模式
particleSyatem2->setPositionType(kCCPositionTypeRelative);
particleSyatem2->setPosition(ccp(200, 160));
addChild(particleSyatem2);
//让当前的Layer永久来回移动
CCActionInterval* move = CCMoveBy::create(3, ccp(290, 0));
CCActionInterval* back = move->reverse();
this->runAction(CCRepeatForever::create(CCSequence::create(move, back, NULL)));