I want loop my particle with duration = 3. that mean after particle play complete with 3 second. it’ll play again (or is there way custom End Event of partilce). How can i do? Thanks so much
Since I don’t think there’s a built in setting in the particle parameters, the way I would do it is programming a sequence with a callfunc to play the particle effect after the DelayTime… Something like this:
var loop = cc.repeatForever(cc.sequence(new cc.DelayTime(3), new cc.CallFunc(function() { this.particle.play(); }, this)));
However, I’ve ran into a problem with calling a play a ParticleSystem object, and I haven’t found a way to simply tell a particle generator lying around to simply play when I wanted to trigger it. Maybe someone else here can tell us how to play a particle again when you need to when you have playOnLoad off and autoRemoveOnFinish off.
The way I had gotten around this problem was to make the particles into a Prefab, then to turn it on I had to cc.instantiate it and set the position to get it to play the 3 second duration on demand. That could work here too… The short of that was something like this:
var self = this;
var createSpark = function (x, y) {
var sparks = cc.instantiate(self.sparkEffect);
sparks.parent = self.boardOverlays;
sparks.setPosition(cc.v2(x, y));
}
Which you can then trigger in that cc.CallFunc of the repeating sequence. Hope that helps. Anyone else know an easier way? It would also help me to know how to play and stop the cc.ParticleSystem from code… Have found nothing in the API.
I solved this problem. I write this class hope usefull for you.
var p;
cc.Class({
extends: cc.Component,
properties: {
loop:true,
// foo: {
// default: null,
// url: cc.Texture2D, // optional, default is typeof default
// serializable: true, // optional, default is true
// visible: true, // optional, default is true
// displayName: 'Foo', // optional
// readonly: false, // optional, default is false
// },
// ...
},
// use this for initialization
onLoad: function () {
p = this.getComponent(cc.ParticleSystem);
},
update:function () {
if(p.particleCount==0){
if(this.loop) p.resetSystem();
else this.destroy();
}
},
});
Really? The .resetSystem(); method starts the particles from the beginning? In the API documentation, the description of resetSystem says “Kill all living particles.”!!! I saw that there weeks ago and I thought nah, that’s wouldn’t be how to restart it… The name makes sense now, but the docs threw me off. That should be corrected in http://www.cocos2d-x.org/docs/api-ref/creator/v1.1.2/classes/ParticleSystem.html#method_resetSystem
I now notice in the example part of it, it does say
// play particle system.
but yeah, that needs to be updated. Thanks. That’s a clever solution by the way, checking if the particleCount is 0 to loop it… Works. Instead of this.destoy(); you could also do p.stopSystem(); if you needed to reuse it and turn on loop again, the destroy wouldn’t let you do that. Good stuff.
Yeah! I’ll instead destroy() to stopSystem(). Thanks @skquark