zoukankan      html  css  js  c++  java
  • cocos2dx上Lua安装与配置1

    在进入主题之前先推荐一本  Lua图书把 http://book.luaer.cn/ 在线看的   感觉还不错  可以当工具书用

    其实在cocos2dx中 已经再带了 一个cocos2dx - Lua的模版

    听说自带的Lua不足够的稳定  所以就自己搭一个了

     1.下载必要软件  Lua 

    我下载的是Lua 5.1.4 感觉网上 5.2 资源不是很多 所以没用

      2.编译 配置 Lua 到 cocos2d

    把Lua 进行 编译 会得到 lua51.lib lua51.dll lua5.1.lib 三个文件 (三个文件的得到方法 发附件 有机会我在附上 编译过程)

    将这三个 文件放到 G:\cocos2d-2.0-rc2-x-2.0.1\Debug.win32       下

    然后 项目右键->属性->配置属性->连接器->输入->附近依赖项->编辑->在上面的输入框中输入 lua5.1.lib ->确定

    然后到 lua 的src 目录下 去寻找 这四个文件

    lauxlib.h    lua.h    luaconf.h    lualib.h

    把它们放到项目中

     3.编写lua的调用 文件 HelloLua.h  HelloLua.cpp

     代码如下:

      HelloLua.h

     1 #pragma once
     2   
     3  extern "C" {
     4  #include "lua.h"
     5  #include "lualib.h"
     6  #include "lauxlib.h"
     7  }
     8   
     9   using namespace std;
    10  class HelloLua
    11  {
    12      lua_State* L;
    13  public:
    14      HelloLua(void);
    15      void loadLua();
    16      virtual ~HelloLua(void);
    17  };

    HelloLua.cpp

     1  #include "HelloLua.h"
     2  #include <iostream>
     3  #include "cocos2d.h"
     4  
     5  USING_NS_CC;
     6  
     7  
     8  HelloLua::HelloLua(void)
     9  {
    10      L=0;
    11  }
    12  
    13  void HelloLua::loadLua(){
    14      
    15      L = lua_open();//初始化lua
    16      luaopen_base(L);//载入lua库
    17      luaL_openlibs(L);
    18  
    19      //const char* filepath = path.c_str();
    20      const char* filepath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("hello.lua");
    21      CCLog(filepath);
    22  
    23      int nRet=luaL_dofile(L, filepath);//运行脚本
    24      
    25      lua_gc(L, LUA_GCCOLLECT, 0);
    26      if(nRet !=0){
    27          
    28          CCLog("%s",lua_tostring(L, -1));
    29          lua_pop(L, 0);
    30      }
    31  
    32      //lua_close(L);
    33      
    34  }
    35  
    36  HelloLua::~HelloLua(void)
    37  {
    38  }

    然后在项目的Resources文件夹中 建立 hello.lua的文件

    用记事本打开写一行代码

    print 'Lua is load'

    4.使用 Lua

    我们到 AppDelegate.cpp中

    在前面引入 文件

    #include "HelloLua"

    在HelloWorld 加载之前 加入这两行

    38  HelloLua *_helloLua = new HelloLua(); 39  _helloLua->loadLua();

    然后运行 项目 会在控制台打印出  'Lua is load'

    好了这篇教程 就到这里了

  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/jaoye/p/2701453.html
Copyright © 2011-2022 走看看