cocos2dx驱动lua

前端之家收集整理的这篇文章主要介绍了cocos2dx驱动lua前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

附带几篇交互方式

http://www.jb51.cc/article/p-snrtiles-bre.html

http://www.jb51.cc/article/p-hsnziskm-bre.html

http://www.jb51.cc/article/p-xywazllr-bre.html

这里针对cocos2dx的实现

1...Scheduler::update 里面直接遍历lua层注册的事件 ,然后派发

#if CC_ENABLE_SCRIPT_BINDING
    //
    // Script callbacks
    //

    // Iterate over all the script callbacks
    if (!_scriptHandlerEntries.empty())
    {
        for (auto i = _scriptHandlerEntries.size() - 1; i >= 0; i--)
        {
            SchedulerScriptHandlerEntry* eachEntry = _scriptHandlerEntries.at(i);
            if (eachEntry->isMarkedForDeletion())
            {
                _scriptHandlerEntries.erase(i);
            }
            else if (!eachEntry->isPaused())
            {
                eachEntry->getTimer()->update(dt);
            }
        }
    }
#endif

2.Node 里面的事件

成员变量

#if CC_ENABLE_SCRIPT_BINDING
    int _scriptHandler;               ///< script handler for onEnter() & onExit(),used in Javascript binding and Lua binding.
    int _updateScriptHandler;         ///< script handler for update() callback per frame,which is invoked from lua & javascript.
    ccScriptType _scriptType;         ///< type of script binding,lua or javascript
#endif


// override me
void Node::update(float fDelta)
{
#if CC_ENABLE_SCRIPT_BINDING
    if (0 != _updateScriptHandler)
    {//派发事件消息到lua
        //only lua use
        SchedulerScriptData data(_updateScriptHandler,fDelta);
        ScriptEvent event(kScheduleEvent,&data);
        ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
    }
#endif
    
    if (_componentContainer && !_componentContainer->isEmpty())
    {
        _componentContainer->visit(fDelta);
    }
}

其他还有很多派发手段,大体思路就是通过ScriptEngine 来派发事件,因为大部分C++类 都通过toLua导出到lua的,导出类自动把lua的handle通过接口ScriptHandlerMgr::getInstance()->addObjectHandler绑定到cpp对象,然后就能相互调用

猜你在找的Cocos2d-x相关文章