本章节学习到了如何使用粒子系统,但是在开发的项目中,我们不会经常使用到,大多时候回将粒子转成帧动画进行播放,降低IO消耗,从而节约开发的成本。直接呈上代码:
var HelloWorldLayer = cc.Layer.extend({
sprite:null,
ctor:function () {
this._super();
var size = cc.winSize;
var particle = ["爆炸粒子","火焰粒子","烟花粒子",
"花粒子","星系粒子","流星粒子",
"漩涡粒子","雪粒子","烟粒子",
"太阳粒子","雨粒子"];
var items = [];
for(var i=0;i<particle.length;i++){
var item = new cc.MenuItemFont(particle[i],this.callback,this);
item.setTag(i*10);
item.setPosition(size.width/2-100,size.height/2-30*i);
items.push(item);
}
var menu = new cc.Menu(items);
menu.setPosition(0,size.height/2-70);
this.addChild(menu);
return true;
},
callback: function (obj) {
var particle;
var size = cc.winSize;
switch(obj.tag){
case 0:
this.removeChildByTag(100);
particle = new cc.ParticleExplosion();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 10:
this.removeChildByTag(100);
particle = new cc.ParticleFire();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 20:
this.removeChildByTag(100);
particle = new cc.ParticleFireworks();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 30:
this.removeChildByTag(100);
particle = new cc.ParticleFlower();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 40:
this.removeChildByTag(100);
particle = new cc.ParticleGalaxy();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 50:
this.removeChildByTag(100);
particle = new cc.ParticleMeteor();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 60:
this.removeChildByTag(100);
particle = new cc.ParticleSpiral();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 70:
this.removeChildByTag(100);
particle = new cc.ParticleSnow();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 80:
this.removeChildByTag(100);
particle = new cc.ParticleSmoke();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 90:
this.removeChildByTag(100);
particle = new cc.ParticleSun();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
case 100:
this.removeChildByTag(100);
particle = new cc.ParticleRain();
particle.setTag(100);
particle.texture=cc.textureCache.addImage("res/ballfire.plist.png");
this.addChild(particle);
particle.setPosition(size.width/2+100,size.height/2);
break;
}
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
上述是cocos2d自带的粒子系统,还可以自定义粒子系统,如:
cc.textureCache.addImage("res/ballfire.plist.png");
var myp=new cc.ParticleSystem("res/ballfire.plist");
this.addChild(myp);
myp.setPosition(cc.winSize.width/2,cc.winSize.height/2);
最后附上作业链接:
http://www.cocoscvp.com/usercode/2016_05_08/c4622e38f30a278640911f24977bc4e0735349ed/
原文链接:https://www.f2er.com/cocos2dx/339510.html