调试过程中引擎定制

调试过程中使用的js引擎是cocos2d-js-for-preview.js

/Applications/CocosCreator.app/Contents/Resources/engine/bin/cocos2d-js-for-preview.js

可以直接在文件中修改,也可以修改js源文件后,生成该文件

# 安装gulp
sudo npm install -g gulp
# 在命令行中进入引擎路径
cd /Applications/CocosCreator.app/Contents/Resources/engine/ npm install
# 生成preview.js
gulp build

可以参考官方文档,如果gulp命令找不到,可以找到gulp文件,然后执行node [gulp路径] build

现在的版本貌似build可以不写,直接执行gulp即可

转发请注明原文地址http://www.cnblogs.com/billyrun/articles/9046872.html

 

发布H5游戏引擎定制

修改了/Applications/CocosCreator.app/Contents/Resources/engine目录中的js源文件后

需要重新生成相应的cocos2d.js或cocos2d.min.js

只需删除该隐藏目录

/Applications/CocosCreator.app/Contents/Resources/engine/bin/.cache 

引擎在执行打包命令时就会自动重新生成cocos2d文件

 

发布原生游戏引擎定制

对于原生游戏,需要修改相应的c++源码

以贝塞尔曲线为例子

若要在bezierTo动作做快进快退()

就需要手动修改ActionInterval对象的_elapsed

H5版本甚至不用加接口,直接修改_elapsed值就可以快进快退(若在动作起始时,需要把_firstTick设置为false)

原生版本需要做以下修改

/Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/2d/CCActionInterval.h

//增加以下代码
inline void setElapsed(float elapsed , bool firstTick) { 
    this->_elapsed = elapsed; 
    this->_firstTick = firstTick;
}

/Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp

//增加以下代码
static bool js_cocos2dx_ActionInterval_setElapsed(se::State& s)
{
    cocos2d::ActionInterval* cobj = (cocos2d::ActionInterval*)s.nativeThisObject();
    SE_PRECONDITION2(cobj, false, "js_cocos2dx_ActionInterval_setElapsed : Invalid Native Object");
    const auto& args = s.args();
    size_t argc = args.size();
    CC_UNUSED bool ok = true;
    if (argc == 2) {
        float arg0 = 0;
        bool arg1 = false;
        ok &= seval_to_float(args[0], &arg0);
        ok &= seval_to_boolean(args[1], &arg1);
        SE_PRECONDITION2(ok, false, "js_cocos2dx_ActionInterval_setElapsed : Error processing arguments");
        cobj->setElapsed(arg0 , arg1);
        return true;
    }
    SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
    return false;
}
SE_BIND_FUNC(js_cocos2dx_ActionInterval_setElapsed)
bool js_register_cocos2dx_ActionInterval(se::Object* obj)
{
    auto cls = se::Class::create("ActionInterval", obj, __jsb_cocos2d_FiniteTimeAction_proto, nullptr);

    cls->defineFunction("getAmplitudeRate", _SE(js_cocos2dx_ActionInterval_getAmplitudeRate));
    cls->defineFunction("initWithDuration", _SE(js_cocos2dx_ActionInterval_initWithDuration));
    cls->defineFunction("setAmplitudeRate", _SE(js_cocos2dx_ActionInterval_setAmplitudeRate));
    cls->defineFunction("getElapsed", _SE(js_cocos2dx_ActionInterval_getElapsed));
    //增加以下一条语句
    cls->defineFunction("setElapsed", _SE(js_cocos2dx_ActionInterval_setElapsed));
    cls->install();
    JSBClassType::registerClass<cocos2d::ActionInterval>(cls);

    __jsb_cocos2d_ActionInterval_proto = cls->getProto();
    __jsb_cocos2d_ActionInterval_class = cls;

    se::ScriptEngine::getInstance()->clearException();
    return true;
}

/Applications/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.hpp

extern se::Object* __jsb_cocos2d_ActionInterval_proto;
extern se::Class* __jsb_cocos2d_ActionInterval_class;

bool js_register_cocos2d_ActionInterval(se::Object* obj);
bool register_all_cocos2dx(se::Object* obj);
SE_DECLARE_FUNC(js_cocos2dx_ActionInterval_getAmplitudeRate);
SE_DECLARE_FUNC(js_cocos2dx_ActionInterval_initWithDuration);
SE_DECLARE_FUNC(js_cocos2dx_ActionInterval_setAmplitudeRate);
SE_DECLARE_FUNC(js_cocos2dx_ActionInterval_getElapsed);
//增加以下语句
SE_DECLARE_FUNC(js_cocos2dx_ActionInterval_setElapsed);

H5版本做以下修改(兼容)

/Applications/CocosCreator.app/Contents/Resources/engine/cocos2d/actions/CCActionInterval.js

/Applications/CocosCreator.app/Contents/Resources/utils/api/engine/cocos2d/actions/CCActionInterval.js

//增加以下代码
setElapsed:function (val , _firstTick) {
    this._elapsed = val;
    this._firstTick = _firstTick;
},

打包原生工程时

先删除jsb-default/jsb-link/jsb-bianry目录

新生成的工程工程即可使用新添加的API

 

参考文献

http://docs.cocos.com/creator/manual/zh/advanced-topics/engine-customization.html#%E5%AE%9A%E5%88%B6-javascript-%E5%BC%95%E6%93%8E

https://blog.csdn.net/LANGZI7758521/article/details/52411916