怎么 暂停和恢复 ParticleSystem粒子动画?

怎么 暂停和恢复 ParticleSystem粒子动画?比如我想暂停正在播放的粒子,然后过了几秒之后恢复粒子的播放,有什么接口或方法控制吗?就好像Animation动画,有pause和resume接口可以暂停和恢复。

大大们,求解答一下:grin:

有相关的 API,stopSystem 和 resetSystem
http://www.cocos.com/docs/creator/api/classes/ParticleSystem.html

1赞

不是要停止或重新启动粒子。就如Animation动画,有pause和resume可以暂停和恢复动画,而不是play和stop。是我表述不明白。。是在粒子播放过程中,需要暂停粒子播放,然后可以恢复粒子播放。

1赞

嗯,那就没办法了

1赞

后续版本会不会添加这方面的需求?

目前还没开始计划

怎么更换file文件? 代码更换:sweat_smile:

现在有计划了吗?一年了

有没有粒子播放结束回调函数呀?

现在有了吗。。。

关于粒子暂停和恢复一种实现方法。
首先查看粒子系统的源码,发现粒子系统的的更新是在组件的lateUpdate

可以考虑重写这个方法,这意味着要去修改引擎的源码,但我不打算直接修改引擎的代码,而是以插件脚本的形式去扩展这个功能。

  1. 新建一个js脚本

  2. 在脚本属性面板勾选 导入为插件 ,点击 应用

  3. 编写插件脚本

    let oldLateUpdate = cc.ParticleSystem.prototype.lateUpdate;

    cc.ParticleSystem.prototype.lateUpdate = function (dt) {
        if (this.isPause) return;
        oldLateUpdate.call(this, dt);
    }

    cc.ParticleSystem.prototype.pause = function () {
        this.isPause = true;
    }

    cc.ParticleSystem.prototype.resume = function () {
        this.isPause = false;
    }

现在就可以在粒子系统中使用pause()resume()暂停和恢复粒子了。

3赞

NB,真实有效

web平台可用,原生平台没有调用lateUpdate,这是为什么?

原生平台的例子发射器并不是用js实现的,使用C++~
所以需要修改C++层面的内容

原生平台粒子pause和resume
通过查找文档发现,引擎对原生平台的粒子系统进行了适配,这里是适配的官方文档引擎定制工作流程
适配的源码在编辑器目录下的builtin\jsb-adapter\engine\jsb-particle.js

1.打开编辑器目录
image
2.找到builtin\jsb-adapter\engine\jsb-particle.js


3.打开jsb-particle.js
image
image
发现适配代码将粒子系统的updatelateUpdate置为null,并且将this._simulator替换为原生层的middleware.ParticleSimulator,粒子不再由lateUpdate更新,而是原生层的middleware.ParticleSimulator更新。pauseresume在原生平台失效的问题已经找到了,现在只需要对middleware.ParticleSimulator改造,使其支持pauseresume接口提供给js层调用就可以了。

4.改造middleware.ParticleSimulator
改造过程涉及一些jsb的知识,这里是官方的文档JSB 2.0使用指南
以android项目为例,找到ParticleSimulator c++源文件,default模板下在打包目录的jsb-default\frameworks\cocos2d-x\cocos\editor-support\particle,link模板下在编辑器目录的cocos2d-x\cocos\editor-support\particle,打开ParticleSimulator.h,参照其他属性和方法的声明方式,声明方法pauseresume以及属性_isPause
image

    ......
    void pause(){this->_isPause=true;};
    void resume(){this->_isPause=false;};
private:
    ......
    bool                            _isPause=false;
    ......

打开ParticleSimulator.cpp,在render中加入以下代码
image

if(this->_isPause)return;

reset中加入以下代码
image

_isPause=false;

接下来需要修改ParticleSimulator相对应的jsb文件,default模板下在打包目录的jsb-default\frameworks\cocos2d-x\cocos\scripting\js-bindings\auto,link模板下在编辑器目录的cocos2d-x\cocos\scripting\js-bindings\auto,打开jsb_cocos2dx_particle_auto.hpp,参照其它方法加入以下代码

......
SE_DECLARE_FUNC(js_cocos2dx_particle_ParticleSimulator_pause);
SE_DECLARE_FUNC(js_cocos2dx_particle_ParticleSimulator_resume);

打开jsb_cocos2dx_particle_auto.cpp,加入以下代码


......
static bool js_cocos2dx_particle_ParticleSimulator_pause(se::State& s)
{
    cocos2d::ParticleSimulator* cobj = (cocos2d::ParticleSimulator*)s.nativeThisObject();
    SE_PRECONDITION2(cobj, false, "js_cocos2dx_particle_ParticleSimulator_pause : Invalid Native Object");
    cobj->pause();
    return  true;
}
SE_BIND_FUNC(js_cocos2dx_particle_ParticleSimulator_pause)

static bool js_cocos2dx_particle_ParticleSimulator_resume(se::State& s)
{
    cocos2d::ParticleSimulator* cobj = (cocos2d::ParticleSimulator*)s.nativeThisObject();
    SE_PRECONDITION2(cobj, false, "js_cocos2dx_particle_ParticleSimulator_emitParticle : Invalid Native Object");
    cobj->resume();
    return true;
}
SE_BIND_FUNC(js_cocos2dx_particle_ParticleSimulator_resume)
......

    cls->defineFunction("pause",_SE(js_cocos2dx_particle_ParticleSimulator_pause));
    cls->defineFunction("resume",_SE(js_cocos2dx_particle_ParticleSimulator_resume));

到这里整个原生层的改造就已经完成了,但还需要对之前的插件脚本进行修改,使其适配web和原生平台

let oldLateUpdate = cc.ParticleSystem.prototype.lateUpdate;

cc.ParticleSystem.prototype.lateUpdate = function (dt) {
    if (this.isPause) return;
    oldLateUpdate.call(this, dt);
}

cc.ParticleSystem.prototype.pause = function () {
    if(cc.sys.isNative){
        this._simulator.pause();
    }else{
        this.isPause = true;
    }
}

cc.ParticleSystem.prototype.resume = function () {
    if(cc.sys.isNative){
        this._simulator.resume();
    }else{
        this.isPause = false;
    }
}

5.总结
还有一种修改方式是 ParticleSimulator只提供一个可读写的isPause属性,使用时直接修改isPause的值。总的来说整个改造原理是非常简单的,但是过程比较繁琐,像pause和resume这种功能还是官方提供比较好

4赞

NB,大佬,我觉得可以做个精华帖,因为貌似很多人都需要粒子的暂停恢复功能

还有单独粒子编程

你是我的神!!