Cocos2D-X 使用粒子(系統)效果

Cocos2D-X 粒子系統相當方便,主要的類別是 CCParticleSystem ,它有許多子類別,凡是 CCParticle開頭的都是,如 CCParticleFire , CCParticleFlower , CCParticleRain等等, 而這些子類別是為了簡化使用,可以直接套用子類別的屬性設定,像是 粒子的生命周期,尺寸,顏色,當然你也可以自己設定這些數值,我們加入1個新的類別(Test_ParticleSystem)來練習

Test_ParticleSystem.h

   1:  #ifndef _TEST_PARTICLESYSTEM
   2:  #define _TEST_PARTICLESYSTEM
   3:  //create by zgh149 2012/07/27
   4:   
   5:  #include "cocos2d.h" //include 需要的標頭檔
   6:  #include "SimpleAudioEngine.h" //include 需要的標頭檔
   7:   
   8:  using namespace cocos2d; //使用名稱空間
   9:  using namespace CocosDenshion; //使用名稱空間
  10:   
  11:  class Test_ParticleSystem : public CCLayerColor // 繼承 CCLayerColor 方便使用畫布顏色
  12:  {
  13:   
  14:  public:
  15:   
  16:      Test_ParticleSystem(void); //建構子
  17:   
  18:      ~Test_ParticleSystem(void); //解構子
  19:   
  20:      virtual bool init(); //初始化
  21:   
  22:      static CCScene* scene(); //建立scene
  23:   
  24:      LAYER_NODE_FUNC(Test_ParticleSystem); //建立layer
  25:   
  26:      bool sleep; //控制此流程的進行
  27:   
  28:      int gameFlow; //此流程的迴圈控制
  29:   
  30:      const static int GAMEFLOW1 =1;
  31:   
  32:      void Run(float dt);//主要迴圈
  33:   
  34:      int GF1Time;//流程時間控制
  35:   
  36:      void visit();//繪圖
  37:   
  38:      void nextScene(CCNode* sender);//下一個流程
  39:   
  40:   
  41:      //測試粒子效果
  42:      CCParticleSystem* explosion;
  43:      void explosionLoad();
  44:      
  45:   
  46:  protected:
  47:   
  48:  private:
  49:   
  50:      void ccTouchesEnded(CCSet* touches, CCEvent* event);//控制觸碰
  51:   
  52:  };
  53:   
  54:  #endif
  55:   
  56:  //create by zgh149

Test_ParticleSystem 和 GameLogoScene 幾乎一模一樣, 去掉 logo 和 vector , 在 42 行加入粒子系統指標 explosion , 43 行為建立 explosion 的方法, 接著是 Test_ParticleSystem.cpp

Test_ParticleSystem.cpp

   1:  #include "Test_ParticleSystem.h"
   2:   
   3:  using namespace cocos2d;
   4:   
   5:  Test_ParticleSystem::Test_ParticleSystem(void)
   6:  {
   7:      
   8:      explosionLoad();
   9:   
  10:      //初始化各項數值
  11:      this->initWithColor(ccc4(0,0,0,255));
  12:      sleep = false;
  13:      gameFlow = 1;
  14:      GF1Time = 0;
  15:   
  16:      //加入schedule
  17:      this->schedule( schedule_selector(Test_ParticleSystem::Run),1); //加入schedule
  18:      
  19:      
  20:  }    
  21:   
  22:  Test_ParticleSystem::~Test_ParticleSystem(void)
  23:  {
  24:      
  25:      CCLog("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  26:  }
  27:   
  28:   
  29:  CCScene* Test_ParticleSystem::scene()
  30:  {
  31:   
  32:      CCScene* scene = NULL;
  33:   
  34:      do 
  35:      {
  36:          
  37:          scene = CCScene::node();
  38:          CC_BREAK_IF(! scene);
  39:   
  40:          
  41:          Test_ParticleSystem *layer = Test_ParticleSystem::node();
  42:          CC_BREAK_IF(! layer);
  43:   
  44:         
  45:          scene->addChild(layer);
  46:      } while (0);
  47:   
  48:      return scene;
  49:  }
  50:   
  51:  void Test_ParticleSystem::Run(float dt)
  52:  {
  53:   
  54:      if(gameFlow==GAMEFLOW1)
  55:      {
  56:          GF1Time++;
  57:          
  58:          
  59:      }
  60:      
  61:  }
  62:   
  63:  void Test_ParticleSystem::ccTouchesEnded(CCSet* touches, CCEvent* event)
  64:  {
  65:   
  66:      CCTouch* touch = (CCTouch*)( touches->anyObject() );
  67:   
  68:      CCPoint location = touch->locationInView();
  69:   
  70:      location = CCDirector::sharedDirector()->convertToGL(location);
  71:   
  72:      CCLog("convertToGLXY  x:%f, y:%f", location.x, location.y);
  73:      
  74:      
  75:  }
  76:   
  77:   
  78:  bool Test_ParticleSystem::init()
  79:  {
  80:      bool bRet = false;
  81:      do 
  82:      {
  83:   
  84:          this->setTouchEnabled(true);
  85:      
  86:   
  87:          bRet = true;
  88:   
  89:      } while (0);
  90:   
  91:      return bRet;
  92:  }
  93:   
  94:  void Test_ParticleSystem::visit()
  95:  {
  96:      if(gameFlow==GAMEFLOW1)
  97:      {    
  98:          this->draw();    
  99:          explosion->visit();
 100:      }
 101:      
 102:  }
 103:   
 104:  void Test_ParticleSystem::nextScene(CCNode* sender)
 105:  {
 106:   
 107:      bool tempa = CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying();
 108:   
 109:      if(tempa)
 110:      {
 111:          
 112:          CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(true);
 113:      }
 114:      
 115:  }
 116:   
 117:   
 118:   
 119:  void Test_ParticleSystem::explosionLoad()
 120:  {
 121:      
 122:      explosion = CCParticleFireworks::create();
 123:      explosion->retain();
 124:      this->addChild(explosion, 10);
 125:      
 126:      explosion->setTexture( CCTextureCache::sharedTextureCache()->addImage("stars.png") );
 127:   
 128:          
 129:  }

第 119 行以上的都和 GameLogoScene 相當類似, 第 119 ~ 129 行為 explosion 的建立方法
第 122 行回傳 CCParticleFireworks 的指標
第 123 行保留 explosion,因為會不斷的使用
第 124 行加入此 layer 中
第 126 行設定材質資源,資源為圖片 stars.png, 當然你也必須在 resource 資料夾放入圖片, CCParticleFireworks會根據此圖片來建立單一粒子
第 8 行在建構子中放入 explosionLoad()方法

執行之後在畫面中央應該會有不斷噴出星星的粒子系統,如下圖













你可以試著修改 112 行的 CCParticleFireworks, 可改為其他粒子效果如 CCParticlerRain 等等,就會出現不同效果唷













以下列出比較常用的方法,如粒子系統的座標,尺寸等等

   1:  void Test_ParticleSystem::explosionLoad()
   2:  {
   3:      
   4:      explosion = CCParticleFireworks::create();
   5:      explosion->retain();
   6:      this->addChild(explosion, 10);
   7:      
   8:      explosion->setTexture( CCTextureCache::sharedTextureCache()->addImage("stars.png") );
   9:   
  10:      explosion->setStartSize(1);//開始尺寸
  11:   
  12:      explosion->setEndSize(20); //結束尺寸
  13:   
  14:      explosion->setDuration(3); //週期
  15:   
  16:      explosion->setStartColor(ccc4f(100,255,100,255));//開始顏色
  17:   
  18:      explosion->setEndColor(ccc4f(100,255,0,255));//結束顏色
  19:   
  20:      explosion->setPosition(ccp(480/2,0));//座標
  21:  }