当前位置:编程学习 > wap >>

Cocos2d-x 的“HelloLua” 深入分析

 我们来看一下Cocos2d-x的HelloLua示例工程。首先编译运行一下这个工程,当然,因为我在HelloWorld工程中改动了CCApplication的run函数和initInstance函数,所以这里要修改一下,与HelloWorld保持一致才能编译成功。哇!一个很COOL的农场游戏。

 

       这几乎是我见过的最令人激动的示例了。农场类游戏两年前可是非常火的!怎么做的,马上来看。

       main.h和main.cpp与HelloWorld无异。不理会了。打开AppDelegate.cpp,可以看到它与HelloWorld工程中的AppDelegate.cpp的明显不同是使用了声音引擎类SimpleAudioEngine和脚本引擎类CCScriptEngineManager。下面我们来分析一下源码。

[cpp] 
<span style="font-size:14px;">//应用程序启动时调用的函数 
bool AppDelegate::applicationDidFinishLaunching() 

    // 初始化显示设备 
    CCDirector *pDirector = CCDirector::sharedDirector(); 
    // 设置显示设备使用initInstance函数创建的OpenGL视窗 
    pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView()); 
    //使用高清模式 
    // pDirector->enableRetinaDisplay(true); 
 
    // 设置显示FPS 
    pDirector->setDisplayFPS(true); 
    //设置设备的显示方向 
    // pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); 
    //设置FPS帧间隔时间 
    pDirector->setAnimationInterval(1.0 / 60); 
 
    // 通过CCLuaEngine的静态函数获取一个LUA脚本引擎实例对象指针 
CCScriptEngineProtocol* pEngine = CCLuaEngine::engine(); 
// 通过CCScripEngineManager的静态函数sharedManager获取单件脚本引擎管理器的实例对象指针,并将上一句创建的LUA脚本引擎实例对象指针设为脚本引擎管理器当前进行管理的脚本引擎。 
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); 
 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
    //在ANDROID平台下,会通过文件处理API类CCFileUtils中的getFileData取得hello.lua文件并读取到char数组中。 
    unsigned long size; 
    char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); 
 
    if (pFileContent) 
    { 
        //将char数组数据放入到一个新的以’\0’结尾的char数组中形成一个LUA脚本字符串 
        char *pCodes = new char[size + 1]; 
        pCodes[size] = '\0'; 
        memcpy(pCodes, pFileContent, size); 
        delete[] pFileContent; 
        //让脚本引擎执行这个LUA脚本字符串 
        pEngine->executeString(pCodes); 
        //释放动态申请的char数组的内存 
        delete []pCodes; 
    } 
#endif 
 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) 
    //如果是Win32,IOS或MARMALADE平台,通过文件处理API类CCFileUtils中的fullPathFromRelativePath函数产生一个hello.lua在当前程序所在目录下的路径。具体实现可参看CCFileUtils_win32.cpp 
    string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); 
    //将这个路径的目录放入到脚本引擎的搜索目录 
    //path.substr会将路径中的目录取出来。 
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); 
//执行这个路径所指向的LUA文件 
    pEngine->executeScriptFile(path.c_str()); 
#endif  
 
    return true; 

</span> 

       我们没有在这里面发现任何关于农场或松鼠的只言片语。我们只知道程序执行了一下“hello.lua”文件。多COOL!

       我已经迫不急待的想要打开hello.lua文件一看究竟了。我们打开工程下的Resource目录,可以发现农场和松鼠的图片,还有一些声音文件,以及我们要找的lua文件,共有两个:hello.lua和hello2.lua。


点击打开hello.lua,我们来分析一下:

[python] 
<span style="font-size:14px;">-- 设定LUAGC的拉圾回收参数 
collectgarbage("setpause", 100) 
collectgarbage("setstepmul", 5000) 
--这里定义一个函数cclog,用来打印字符串参数 
local cclog = function(...) 
    print(string.format(...)) 
end 
--将hello2.lua包含进来,hello2.lua定义了myadd函数的实现 
require "hello2" 
--这里调用cclog打印一个和的结果,并没什么实际用处,可略过 
cclog("result is " .. myadd(3, 5)) 
 
--------------- 
--通过CCDirector:sharedDirector()来取得显示设备实例对象,并调用其getWinSize函数取得窗口大小给变量winSize 
local winSize = CCDirector:sharedDirector():getWinSize() 
 
-- 定义createDog函数创建松鼠 
local function creatDog() 
    --定义两个变量为每一帧图块的宽高 
    local frameWidth = 105 
    local frameHeight = 95 
 
-- 创建松鼠的动画 
-- 先使用CCTextureCache:sharedTextureCache()取得纹理块管理器,将dog.png放入纹理块管理器产生一张纹理返回给变量textureDog 
local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png") 
--创建一个矩形返回给变量rect  
local rect = CCRectMake(0, 0, frameWidth, frameHeight) 
--由这个矩形从纹理上取出图块产生一个CCSpriteFrame指针返回给变量frame0 
local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect) 
--换一个新的位置的矩形返回给变量rect中 
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight) 
--由第二个矩形从纹理上取出图块产生一个CCSpriteFrame指针返回给变量frame1 
local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect) 
    --从frame0产生一个精灵返回给变量spriteDog(在C++中是CCSprite指针) 
local spriteDog = CCSprite:spriteWithSpriteFrame(frame0) 
--设置初始化时 
spriteDog.isPaused = false 
--设置精灵的位置在左上的位置 
    spriteDog:setPosition(0, winSize.height / 4 * 3) 
 
    --生成一个设定大小为2的CCMutableArray类的实例对象。用来存储CCSpriteFrame指针,将其指针返回给变量animFrames 
local animFrames = CCMutableArray_CCSpriteFrame__:new(2) 
--调用addObject将frame0和frame1放入animFrames 
    animFrames:addObject(frame0) 
    animFrames:addObject(frame1) 
    --由容器类实例对象的指针animFrames创建一个动画帧信息对象,设定每0.5秒更新一帧,返回动画帧信息对象指针给变量animation 
local animation = CCAnimation:animationWithFrames(animFrames, 0.5) 
--由animation创建出一个动画动作,将这个动画动作的指针给变量animate 
local animate = CCAnimate:act

补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,