cocos2dx是怎么使用lua引擎的

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

第一步:看cocos2dx调用lua的最初切入点:

bool AppDelegate::applicationDidFinishLaunching()
{
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480,320,kResolutionNoBorder);
    pDirector->setDisplayStats(true);
    pDirector->setAnimationInterval(1.0 / 60);

    // register lua engine
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();//从这里开始了lua旅程。
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);//保存刚才新建的lua引擎对象,以便别处调用。

    std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");//CCFileUtils工具类一定封装了文件搜索路径。 
    //接下来直接执行了脚本,跟句helloCpp工程的实现,这里本该是类似
    //CCScene *pScene = HelloWorld::scene();
    //pDirector->runWithScene(pScene);
    //这样的代码,但这里并没有这样的代码,可以猜测:hello.lua中一定是实现了类似这样的一些功能,事实上,一定是这样。
    //---因为需要添加的这些东西scene、other nodes都是一些c++类的对象,
    //既然hello.lua能完成这些工作,那么lua也一定是有能力访问并操作了这些类与对象,
    //换句话说,也就是一定存在一种机制,让c++暴漏一些接口供lua调用,
    //亦即:lua可以访问cocos2dx里面的东西,并且,一定封装的很好用,否则没人用了。
    pEngine->executeScriptFile(path.c_str()); 
    return true;
}

第二步:看看CCLuaEngine到底干了点啥

class CCLuaEngine : public CCScriptEngineProtocol
{
public:
    static CCLuaEngine* defaultEngine(void);    
    virtual ~CCLuaEngine(void);
    virtual ccScriptType getScriptType() {
        return kScriptTypeLua;
    };
    CCLuaStack *getLuaStack(void) {
        return m_stack;
    }
    //-----------new in me 1
    virtual void addSearchPath(const char* path);
    //-----------new in me 2
    virtual void addLuaLoader(lua_CFunction func);
    //----------------------------inherit from CCScriptEngineProtocol------------------
    //==================这些函数好像都是纯虚函数父类中===========================
    //--------下面这几个函数基本都是调用【CCLuaStack *m_stack;】的成员方法来实现的
    virtual void removeScriptObjectByCCObject(CCObject* pObj);
    virtual void removeScriptHandler(int nHandler);
    virtual int reallocateScriptHandler(int nHandler);
    virtual int executeString(const char* codes);
    virtual int executeScriptFile(const char* filename);
    virtual int executeGlobalFunction(const char* functionName);
    //--------------------------------end--------------------------------
    virtual int executeNodeEvent(CCNode* pNode,int nAction);
    virtual int executeMenuItemEvent(CCMenuItem* pMenuItem);
    virtual int executeNotificationEvent(CCNotificationCenter* pNotificationCenter,const char* pszName);
    virtual int executeCallFuncActionEvent(CCCallFunc* pAction,CCObject* pTarget = NULL);
    virtual int executeSchedule(int nHandler,float dt,CCNode* pNode = NULL);
    virtual int executeLayerTouchesEvent(CCLayer* pLayer,int eventType,CCSet *pTouches);
    virtual int executeLayerTouchEvent(CCLayer* pLayer,CCTouch *pTouch);
    virtual int executeLayerKeypadEvent(CCLayer* pLayer,int eventType);
    virtual int executeAccelerometerEvent(CCLayer* pLayer,CCAcceleration* pAccelerationValue);
    virtual int executeEvent(int nHandler,const char* pEventName,CCObject* pEventSource = NULL,const char* pEventSourceClassName = NULL);
    int executeTableViewEvent(int nEventType,cocos2d::extension::CCTableView* pTableView,void* pValue = NULL,CCArray* pResultArray = NULL);
    virtual int executeEventWithArgs(int nHandler,CCArray* pArgs);
    virtual bool handleAssert(const char *msg);
    virtual bool parseConfig(CCScriptEngineProtocol::ConfigType type,const std::string& str);
    //----------------------------inherit from CCScriptEngineProtocol end--------------
private:
    CCLuaEngine(void)
    : m_stack(NULL)
    {}
    bool init(void);
    
    static CCLuaEngine* m_defaultEngine;
    CCLuaStack *m_stack;
};

第三步:CCLuaEngine类中有一个指针成员变量m_stack,我们先来看看它的定义,注意下CCLuaStack和CCLuaEngine的区别于联系

class CCLuaStack : public CCObject
{
public:
    static CCLuaStack *create(void);
    static CCLuaStack *attach(lua_State *L);
    lua_State* getLuaState(void) {
        return m_state;
    }
    virtual void addSearchPath(const char* path);
    virtual void addLuaLoader(lua_CFunction func);
    virtual void removeScriptObjectByCCObject(CCObject* pObj);
    virtual void removeScriptHandler(int nHandler);
    virtual int reallocateScriptHandler(int nHandler);
    virtual int executeString(const char* codes);
    virtual int executeScriptFile(const char* filename);
    virtual int executeGlobalFunction(const char* functionName);

    virtual void clean(void);
    virtual void pushInt(int intValue);
    virtual void pushFloat(float floatValue);
    virtual void pushBoolean(bool boolValue);
    virtual void pushString(const char* stringValue);
    virtual void pushString(const char* stringValue,int length);
    virtual void pushNil(void);
    virtual void pushCCObject(CCObject* objectValue,const char* typeName);
    virtual void pushCCLuaValue(const CCLuaValue& value);
    virtual void pushCCLuaValueDict(const CCLuaValueDict& dict);
    virtual void pushCCLuaValueArray(const CCLuaValueArray& array);    
    virtual bool pushFunctionByHandler(int nHandler);
    virtual int executeFunction(int numArgs);
    virtual int executeFunctionByHandler(int nHandler,int numArgs);
    virtual int executeFunctionReturnArray(int nHandler,int nNumArgs,int nNummResults,CCArray* pResultArray);
    virtual bool handleAssert(const char *msg);
protected:
    CCLuaStack(void)
    : m_state(NULL),m_callFromLua(0)
    {}
    bool init(void);
    bool initWithLuaState(lua_State *L);
    
    lua_State *m_state;
    int m_callFromLua;
};
原文链接:https://www.f2er.com/cocos2dx/344258.html

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