首页 > 代码库 > Cocos2d-x粒子系统

Cocos2d-x粒子系统

CCparticleSystem类封装实现对粒子的控制与调度,其中操作包括有:

1、产生粒子

2、更新粒子状态

3、回收无效的粒子

CCparticleSystem派生出CCParticleSystemPoint(重力式粒子系统)和CCParticleSystemQuad(放射性粒子系统)

粒子效果有:

CCParticleExplosion (爆炸粒子效果)
CCParticleFireworks (烟花粒子效果)
CCParticleFire (火焰粒子效果)
CCParticleFlower (花束粒子效果)
CCParticleGalaxy (星系粒子效果)
CCParticleMeteor (流星粒子效果)
CCParticleSpiral (漩涡粒子效果)
CCParticleSnow (雪粒子效果)
CCParticleSmoke (烟粒子效果)
CCParticleSun (太阳粒子效果)
CCParticleRain (雨粒子效果)

实例:

1、通过制定纹理图片创建粒子效果

	CCParticleSystem *particle = CCParticleExplosion::create();
	particle->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	particle->setAutoRemoveOnFinish(true);
	this->addChild(particle);

2、根据属性创建放射性粒子系统(无重力)CCParticleSystemQuad

CCParticleSystem *m_emitter;
	m_emitter = new CCParticleSystemQuad();
	m_emitter->initWithTotalParticles(300);
	//设置纹理
	m_emitter->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	m_emitter->setDuration(-1);
	//对齐
	m_emitter->setGravity(CCPointZero);
	//角度
	m_emitter->setAngle(90);
	m_emitter->setAngleVar(360);
	//速度
	m_emitter->setSpeed(160);
	m_emitter->setSpeedVar(20);
	//生命周期
	m_emitter->setLife(4);
	m_emitter->setLifeVar(1);
	//粒子旋转
	m_emitter->setStartSpin(0);
	m_emitter->setStartSizeVar(0);
	m_emitter->setEndSpin(0);
	m_emitter->setEndSpinVar(0);
 	//粒子颜色
	ccColor4F startColor = {0.5f,0.5f,0.5f,0.5f};
	m_emitter->setStartColor(startColor); 
	ccColor4F startColor2 = {0.5f,0.5f,0.5f,1.0f};
	m_emitter->setStartColorVar(startColor2);
	ccColor4F startColor3 =	{0.1f,0.1f,0.1f,0.2f};
	m_emitter->setEndColor(startColor3); 
	ccColor4F startColor4 =	 {0.1f,0.1f,0.1f,0.2f} ;
	m_emitter->setEndColorVar(startColor4); 
    //粒子尺寸
	m_emitter->setStartSize(20.0f);  
	m_emitter->setStartSizeVar(10.0f);   
	m_emitter->setEndSize(32.0f);  
	m_emitter->setEndSizeVar(8.0f);
	//每秒出现粒子数
	m_emitter->setEmissionRate(m_emitter->getTotalParticles()/m_emitter->getLife());  // 每秒产生粒子数 =  粒子总数/存活时间 ;
	//位置
	m_emitter->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
	this->addChild(m_emitter);
效果: