cocos2dx粒子系统

简介

术语粒子系统关系到计算机图形学技术,使用大量非常小的sprite或其他图像对象来模拟特定种类的”模糊”现象,通过常规的渲染技术是很难制造的——一般来说有混乱系统、自然现象、或者活学反应引起的过程。

Point vs Quad

在Cocos2d-x早期版本中,Cocos2d-x里粒子系统有两种类型:Quad 和 Point粒子系统:

CCParticleSystemQuad有很多特性是CCParticleSystemPoint不支持的:

- Spinning particles:粒子可以绕自身的轴旋转。CCParticleSystemPoint忽略了该属性。
- 粒子可以为任意大小。在CCParticleSystemPoint中,如果大小大于64时,就视为64。
- 通过scale属性可以缩放整个系统。

由于CCParticleSystemPoint不支持CCParticleBatchNode.所以,CCParticleSystemPoint已经从cocos2d-x粒子系统里移除了.

CCParticleBatchNode

CCParticleBatchNode可以引用且只可以引用1个texture(一个图片文件,一个texture图集).只有CCParticleSystem可以增加到CCSpriteBatchNode中.
所有增加到CCSpriteBatchNode中的CCParticleSystem是在OpenGL ES调用绘图函数时绘制的.
如果CCParticleSystem没有增加到CCParticleBatchNode中,OpenGL ES会调用每个粒子系统的绘图函数,这样做是低效的.

创建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-&gt;m_nEmitterMode = kCCParticleModeGravity; // Gravity Mode: gravity this-&gt;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-&gt;m_nEmitterMode = kCCParticleModeRadius; // Radius Mode: startRadius this-&gt;modeB.startRadius = 0; this-&gt;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. 使用重力模式.



//建立一个粒子 系统  
CCParticleSystem* pParticleSystem=new CCParticleSystemQuad();  
//产生300个粒子  
pParticleSystem->initWithTotalParticles(10);  
//设置粒子图片  
pParticleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("star.png"));  
//设置粒子系统持续时间  
pParticleSystem->setDuration(-1);  //秒,-1为永久
//设置重力方向  
pParticleSystem->setGravity(CCPointZero);  
//设置角度,角度变化率  
pParticleSystem->setAngle(0);  
pParticleSystem->setAngleVar(360);  
//设置运动速度,运动速度的变化率  
pParticleSystem->setSpeed(50);  
pParticleSystem->setSpeedVar(10);  
//设置径向加速度,径向加速度的变化率  
pParticleSystem->setRadialAccel(70);  
pParticleSystem->setRadialAccelVar(10);  
//设置切向加速度,径向加速度的变化率  
pParticleSystem->setTangentialAccel(80);  
pParticleSystem->setTangentialAccelVar(0);  
//设置粒子初始位置,位置变化率  
pParticleSystem->setPosition(mSize.width/2,mSize.height/2);  
pParticleSystem->setPosVar(ccp(100,100));  
//设置粒子存在时间,时间变化率  
pParticleSystem->setLife(2);  
pParticleSystem->setLifeVar(0.3);  
//设置粒子初始颜色,颜色变化率  
ccColor4F cccStart={0.5,0.5,0.5,1.0};  
pParticleSystem->setStartColor(cccStart);  
pParticleSystem->setStartColorVar(cccStart);  
//设置粒子结束颜色,颜色变化率  
ccColor4F cccEnd={0.5,0.5,0.5,1.0};  
pParticleSystem->setStartColor(cccEnd);  
pParticleSystem->setStartColorVar(cccEnd);  
//设置粒子初始大小,大小变化率  
pParticleSystem->setStartSize(10);  
pParticleSystem->setStartSizeVar(20);  
//设置粒子结束大小,大小变化率  
pParticleSystem->setStartSize(12);  
pParticleSystem->setStartSizeVar(12);  
//设置每秒产生粒子数  
pParticleSystem->setEmissionRate(pParticleSystem->getTotalParticles()/pParticleSystem->getLife());  
  
this->addChild(pParticleSystem,1);  
 

【二】:函数


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)));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值