Cocos2d-x-Lua演示样例项目HelloLua


本篇博客介绍Cocos2d-x中Lua的实例项目,就是使用Cocos2d-x创建的初始项目执行所呈现的农场,这里笔者取名为HelloLua。本篇博客会具体在代码中解析Cocos2d-x 3.1.1创建的Lua项目中实例,一些API的使用。

注:本演示样例项目在Mac系统下创建

首先我们来创建一个Cocos2d-x Lua项目,在命令敲入相似以下命令

cocos new HelloLua -p com.wwj.hellolua -l lua -d ~/Cocos2dxProj

这样我们就在Cocos2dxProj文件夹下创建了一个名叫HelloLua的Lua项目

进入我们runtime-src文件夹下打开proj.ios_mac文件夹。双击使用Xcode打开我们的项目:

Cocos2d-x-Lua演示样例项目HelloLua

使用Xcode对我们的Lua项目进行编译并执行,就会呈现一个以下效果的演示样例游戏:

    Cocos2d-x-Lua演示样例项目HelloLua

看完效果图之后,来看看我们XCode里面的项目文件结构,例如以下图:

Cocos2d-x-Lua演示样例项目HelloLua

以上画圈的有main.cpp、AppDelegate.h、AppDelegate.cpp、main.lua、hello2.lua

我们以下一个一个来看:

首先来看main.cpp文件,这个文件就是我们程序的入口文件,程序的执行时从这里開始的

》》main.cpp

以上代码我们能够看到,在main.cpp里,通过#include引入了两个头文件,一个是AppDelegate.h、一个是cocos2d.h。

定义了我们程序的入口方法main,通过执行Application::getInstance()->run()方法来执行我们的程序。


接着我们来看AppDelegate.h和AppDelegate.cpp,这里两个文件用于控制整个游戏的生命周期

>>>AppDelegate.h

#ifndef __APP_DELEGATE_H__
#define __APP_DELEGATE_H__

#include "cocos2d.h"

/**
@brief    The cocos2d Application.

The reason for implement as private inheritance is to hide some interface call by Director.
*/
class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();
    virtual ~AppDelegate();

    /**
    @brief    Implement Director and Scene init code here.
    @return true    Initialize success, app continue.
    @return false   Initialize failed, app terminate.
    */
    virtual bool applicationDidFinishLaunching();

    /**
    @brief  The function be called when the application enter background
    @param  the pointer of the application
    */
    virtual void applicationDidEnterBackground();

    /**
    @brief  The function be called when the application enter foreground
    @param  the pointer of the application
    */
    virtual void applicationWillEnterForeground();
};

#endif  // __APP_DELEGATE_H__

>>>AppDelegate.cpp

FPS也就是屏幕每秒重绘的次数。即每秒帧速率。在游戏开发阶段。能够方便地确定游戏执行是否流畅。 director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this // 设置绘制间隔 director->setAnimationInterval(1.0 / 60); // 获得Lua引擎实例 auto engine = LuaEngine::getInstance(); // 设置脚本引擎 ScriptEngineManager::getInstance()->setScriptEngine(engine); // 执行main.lua脚本 if (engine->executeScriptFile("src/main.lua")) { return false; } return true; } // This function will be called when the app is inactive. When comes a phone call,it's be invoked too // 当应用程序将要进入后台时,会调用这种方法 void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again // 该方法与applicationDidEnterBackground() 成对出现。在应用程序回到前台时被调用 void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); }


我们在AppDelegate类其中能够找到执行我们Lua脚本的方法,以下来看一下main.lua这个文件,我们屏幕显示的逻辑实现全部在这个文件里能够看到:

>>>main.lua

两个经常使用的debug处理函数:debug.debug和debug.traceback 前者给出Lua的提示符,你能够自己动手察看发生错误时的情况; 后者通过traceback创建很多其它的错误信息,也是控制台解释器用来构建错误信息的函数。 --]] local status, msg = xpcall(main, __G__TRACKBACK__) if not status then error(msg) end



这是Cocos2d-x 3.1.1所使用的代码,代码中的凝视已经非常具体了,笔者在这里就不多说,希望大家能认真阅读,跟笔者一起尽快入门Lua在Cocos2dx中的使用。










相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-15
  • 2021-09-18
  • 2021-08-14
  • 2021-06-01
  • 2022-12-23
相关资源
相似解决方案