一、lua创建.pkg文件操作:


(1)新建文件,选择other,规则:

开始编写pkg文件,还记得README里的规则吗?再看一次:

   1) enum keeps the same    //枚举类型保留不变

   2) remove CC_DLL for the class defines, pay attention to multi inherites      //不要使用CC_DLL,改用多继承

   3) remove inline keyword for declaration and implementation                //删除内置变量?

   4) remove public protect and private     //不要用访问限定词

   5) remove the decalration of class member variable               //不要成员变量

   6) keep static keyword         //保留静态关键词

   7) remove memeber functions that declared as private or protected //public的函数


(2)改动文件名为a.pkg  然后打开它所在的文件夹,复制到tools/tolua++中

(3)解压tools/tolua++中的文件tolua++.Mac.zip  更改build.sh中的:第一个-》TOLUA=/Users/student/Desktop/cocos2d-x-2.2.2/tools/tolua++/tolua++

最后一个:cd ${SCRIPT_DIR}

${TOLUA} -L basic.lua -o /Users/student/Desktop/lua/LuaCocos2d.cpp Cocos2d.pkg



二、lua加入.pkg文件操作:

cd /Users/student/Desktop/cocos2d-x-2.2.2/tools/tolua++ 

ls

vi Cocos2d.pkg

i

加入.pkg的文件

ESC

shift+ZZ

pwd

make


三、运行lua脚本(类的使用)

LuaCocos2d.cpp文件替换掉cocos2dx_support的LuaCocos2d.cpp  并将其加入到项目中。创建一个.lua的文件,进行测试。


例如:

(1)MySprite.h

#ifndef __TestLua__MySprite__

#define __TestLua__MySprite__


#include <iostream>

#include "cocos2d.h"

USING_NS_CC;


class MySprite:public CCSprite

{

public:

    static MySprite* createMS(const char * fileName);

};



#endif /* defined(__TestLua__MySprite__) */

(2)MySprite.cpp

#include "MySprite.h"


MySpriteMySprite::createMS(const char * fileName)

{

    MySprite *sp=new MySprite();

    if(sp && sp->initWithFile(fileName))

    {

        sp->setPosition(ccp(100,100));

        sp->autorelease();

        return sp;

    }

    CC_SAFE_DELETE(sp);

    return NULL;

}

(3)MySprite.pkg

//包含的MySprite的函数

class MySprite:public CCSprite

{

    static MySprite* createMS(const char * fileName);

};


(4)Test.lua

local scene=CCScene:create()

local layer=CCLayer:create()

scene:addChild(layer)

local sp=MySprite:createMS("menu1.png")

layer:addChild(sp)


CCDirector:sharedDirector():runWithScene(scene)

(5)AppDelegate.cpp中

bool AppDelegate::applicationDidFinishLaunching()

{

    // initialize director

    CCDirector *pDirector = CCDirector::sharedDirector();

    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());


    // turn on display FPS

    pDirector->setDisplayStats(true);


    // set FPS. the default value is 1.0/60 if you don't call this

    pDirector->setAnimationInterval(1.0 / 60);


    // register lua engine

    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();

    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);


    CCLuaStack *pStack = pEngine->getLuaStack();

    lua_State *tolua_s = pStack->getLuaState();

    tolua_extensions_ccb_open(tolua_s);//获取lua的执行环境

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    pStack = pEngine->getLuaStack();

    tolua_s = pStack->getLuaState();

    tolua_web_socket_open(tolua_s);

#endif

    

#if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)

    CCFileUtils::sharedFileUtils()->addSearchPath("script");

#endif

    

//lua的路径 并执行

    std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("Test.lua");

    pEngine->executeScriptFile(path.c_str());


    return true;

}