cocos2dx驱动lua

梦想游戏人
目录:
Cocos2dx

附带几篇交互方式

https://my.oschina.net/kkkkkkkkkkkkk/blog/825686

https://my.oschina.net/kkkkkkkkkkkkk/blog/518154

https://my.oschina.net/kkkkkkkkkkkkk/blog/746684

这里针对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对象,然后就能相互调用

Scroll Up