1. 程式人生 > >cocos2d-x 粒子編輯器和使用

cocos2d-x 粒子編輯器和使用


設定屬性實現自定義粒子系統,通過這種方式完全可以實現我們說需要的各種效果的粒子系統。

使用ParticleSystemQuad 自定義粒子系統至少有兩種方式可以實現 :程式碼建立和 plist 檔案建立。

程式碼建立:

主要程式碼如下


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::createWithTotalParticles(200);    ①
 
//設定粒子紋理圖片
particleSystem->setTexture(TextureCache::getInstance()->addImage("fire.png"));    ②
//設定發射粒子的持續時間-1表示永遠持續
particleSystem->setDuration(-1);
//設定粒子的重力方向
particleSystem->setGravity(Point(0,-240));
 
//設定角度以及偏差
particleSystem->setAngle(90);
particleSystem->setAngleVar(360);
 
//設定徑向加速度以及偏差
particleSystem->setRadialAccel(50);
particleSystem->setRadialAccelVar(0);
 
//設定粒子的切向加速度以及偏差
particleSystem->setTangentialAccel(30);
particleSystem->setTangentialAccelVar(0);
 
// 設定粒子初始化位置偏差
particleSystem->setPosVar(Point(400,0));
 
//設定粒子生命期以及偏差
particleSystem->setLife(4);
particleSystem->setLifeVar(2);
 
//設定粒子開始時候旋轉角度以及偏差
particleSystem->setStartSpin(30);
particleSystem->setStartSpinVar(60);
 
//設定結束時候的旋轉角度以及偏差
particleSystem->setEndSpin(60);
particleSystem->setEndSpinVar(60);
 
//設定開始時候的顏色以及偏差
particleSystem->setStartColor(Color4F(1,1,1,1));
//設定結束時候的顏色以及偏差
particleSystem->setEndColor(Color4F(1,1,1,1));
 
//設定開始時候粒子大小以及偏差
particleSystem->setStartSize(30);
particleSystem->setStartSizeVar(0);
 
//設定粒子結束時候大小以及偏差
particleSystem->setEndSize(20.0f);
particleSystem->setEndSizeVar(0);
 
//設定每秒鐘產生粒子的數量
particleSystem->setEmissionRate(100);
 
particleSystem->setPosition(Point(visibleSize.width/2, visibleSize.height + 50));
 
this->addChild(particleSystem);
 
     return true;
}


上述第①行程式碼ParticleSystemQuad::createWithTotalParticles(200) 是建立 ParticleSystemQuad 物件 ,靜態createWithTotalParticles 函式是通過指定初始粒子數來建立粒子物件。
第②行程式碼是指定粒子的紋理,TextureCache::getInstance()->addImage("snow.png") 語句可以通過指定的紋理 圖片建立紋理物件Texture2D , 貼圖的紋理圖片寬高必須是2的n次冪,大小不要超過64x64 畫素,在美工設計紋理圖片時候,不用關注太多細節