Cocos2d-JS內置粒子系統

從類圖中咱們能夠看到,Cocos2d-JS中有內置的11種粒子,這些粒子的屬性都是預先定義好的,咱們也能夠在程序代碼中單獨修改某些屬性,咱們在上一節的實例中都已經實現了這些屬性的設置。
內置粒子系統
內置的11種粒子系統說明以下: 
ParticleExplosion。爆炸粒子效果,屬於半徑模式。
ParticleFire。火焰粒子效果,屬於重力徑模式。
ParticleFireworks。煙花粒子效果,屬於重力模式。
ParticleFlower。花粒子效果,屬於重力模式。
ParticleGalaxy。星系粒子效果,屬於半徑模式。
ParticleMeteor。流星粒子效果,屬於重力模式。
ParticleSpiral。漩渦粒子效果,屬於半徑模式。
ParticleSnow。雪粒子效果,屬於重力模式。
ParticleSmoke。煙粒子效果,屬於重力模式。
ParticleSun。太陽粒子效果,屬於重力模式。
ParticleRain。雨粒子效果,屬於重力模式。
這11種粒子的屬性,根據它的發射模式不一樣,效果不一樣。
實例:內置粒子系統
下面咱們經過一個實例演示一下這11種內置粒子系統。這個實例如圖所示,左圖是一個操做菜單場景,選擇菜單能夠進入到上圖動做場景,在下圖動做場景中演示選擇的粒子系統效果,點擊右下角返回按鈕能夠返回到菜單場景。

html

內置粒子系統實例瀏覽器

下面咱們重點介紹一下場景MyActionScene,它的MyActionScene.js代碼以下:
微信

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片函數

  1. var MyActionLayer = cc.Layer.extend({  網站

  2.     flagTag: 0,                         // 操做標誌  this

  3.     pLabel: null,                                                       ①  spa

  4.     ctor: function (flagTag) {  .net

  5.        

  6.         this._super();  code

  7.         this.flagTag = flagTag;  orm

  8.   

  9.   

  10.         cc.log("MyActionLayer init flagTag " + this.flagTag);  

  11.   

  12.   

  13.         var size = cc.director.getWinSize();  

  14.   

  15.   

  16.         var backMenuItem = new cc.LabelBMFont("<Back", res.fnt_fnt);  

  17.         var backMenuItem = new cc.MenuItemLabel(backMenuItem, this.backMenu, this);  

  18.         backMenuItem.x = size.width - 100;  

  19.         backMenuItem.y = 100;  

  20.   

  21.   

  22.         var mn = cc.Menu.create(backMenuItem);  

  23.         mn.x = 0;  

  24.         mn.y = 0;  

  25.         mn.anchorX = 0.5;  

  26.         mn.anchorY = 0.5;  

  27.         this.addChild(mn);  

  28.   

  29.   

  30.         this.pLabel =  new cc.LabelBMFont("", res.fnt_fnt);  

  31.         this.pLabel.x = size.width /2;  

  32.         this.pLabel.y = size.height  - 50;  

  33.         this.addChild(this.pLabel, 3);  

  34.   

  35.   

  36.         return true;  

  37.     },  

  38.     backMenu: function (sender) {  

  39.         cc.director.popScene();  

  40.     },  

  41.     onEnterTransitionDidFinish: function () {  

  42.         cc.log("Tag = " + this.flagTag);  

  43.         var sprite = this.getChildByTag(SP_TAG);  

  44.         var size = cc.director.getWinSize();  

  45.   

  46.   

  47.        var system;  

  48.         switch (this.flagTag) {                                         ②  

  49.             case ActionTypes.kExplosion:  

  50.                 system = new cc.ParticleExplosion();  

  51.                 this.pLabel.setString("Explosion");  

  52.                 break;  

  53.             case ActionTypes.kFire:  

  54.                 system = new cc.ParticleFire();  

  55.                             system.texture = cc.textureCache.addImage(res.s_fire);              ③  

  56.                 this.pLabel.setString("Fire");  

  57.                 break;  

  58.             case ActionTypes.kFireworks:  

  59.                 system = new cc.ParticleFireworks();  

  60.                 this.pLabel.setString("Fireworks");  

  61.                 break;  

  62.             case ActionTypes.kFlower:  

  63.                 system = new cc.ParticleFlower();  

  64.                 this.pLabel.setString("Flower");  

  65.                 break;  

  66.             case ActionTypes.kGalaxy:  

  67.                 system = new cc.ParticleGalaxy();  

  68.                 this.pLabel.setString("Galaxy");  

  69.                 break;  

  70.             case ActionTypes.kMeteor:  

  71.                 system = new cc.ParticleMeteor();  

  72.                 this.pLabel.setString("Meteor");  

  73.                 break;  

  74.             case ActionTypes.kRain:  

  75.                 system = new cc.ParticleRain();  

  76.                 this.pLabel.setString("Rain");  

  77.                 break;  

  78.             case ActionTypes.kSmoke:  

  79.                 system = new cc.ParticleSmoke();  

  80.                 this.pLabel.setString("Smoke");  

  81.                 break;  

  82.             case ActionTypes.kSnow:  

  83.                 system = new cc.ParticleSnow();  

  84.                 this.pLabel.setString("Snow");  

  85.                 break;  

  86.             case ActionTypes.kSpiral:  

  87.                 system = new cc.ParticleSpiral();  

  88.                 this.pLabel.setString("Spiral");  

  89.                 break;  

  90.             case ActionTypes.kSun:  

  91.                 system = new cc.ParticleSun();  

  92.                 this.pLabel.setString("Sun");  

  93.                 break;                                                  ④  

  94.         }  

  95.   

  96.   

  97.         system.x = size.width /2;  

  98.         system.y = size.height /2;  

  99.   

  100.   

  101.         this.addChild(system);  

  102.     }  

  103. });  

  104.   

  105.   

  106. var MyActionScene = cc.Scene.extend({  

  107.     onEnter: function () {  

  108.         this._super();  

  109.     }  

  110. });  



在頭文件中第①行代碼定義了LabelBMFont類型的成員變量pLabel,用來在場景中顯示粒子系統的名稱。
咱們在MyActionLayer的onEnterTransitionDidFinish函數中建立粒子系統對象,而不是在MyActionLayer的onEnter函數建立,這是由於MyActionLayer的onEnter函數調用時,場景尚未顯示,若是在該函數中建立爆炸等顯示一次的粒子系統,等到場景顯示的時候,爆炸已經結束了,咱們會看不到效果。
代碼第②~④行建立了11種粒子系統,這裏建立粒子系統時候都採用了它們的默認屬性值。其中this.pLabel.setString("XXX")函數是爲場景中標籤設置內容,這樣在進入場景後能夠看到粒子系統的名稱。

另外,若是在Web瀏覽器中運行還須要爲粒子系統添加紋理,咱們只在代碼第③行添加了火粒子紋理,其它的粒子紋理添加相似。


更多內容請關注最新Cocos圖書《Cocos2d-x實戰:JS卷——Cocos2d-JS開發

本書交流討論網站:http://www.cocoagame.net

歡迎加入Cocos2d-x技術討論羣:257760386

更多精彩視頻課程請關注智捷課堂Cocos課程:http://v.51work6.com

智捷課堂現推出Cocos會員,敬請關注:http://v.51work6.com/courseInfoRedirect.do?action=netDetialInfo&courseId=844465&amp;categoryId=0

《Cocos2d-x實戰 JS卷》現已上線,各大商店均已開售:

京東:http://item.jd.com/11659698.html

歡迎關注智捷iOS課堂微信公共平臺,瞭解最新技術文章、圖書、教程信息

相關文章
相關標籤/搜索