博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x学习笔记
阅读量:4321 次
发布时间:2019-06-06

本文共 3856 字,大约阅读时间需要 12 分钟。

1.新建的lua项目使用print()函数无法打印日志的问题

   解决:是因为lua项目用的是babelua插件,本身不能输出日志到窗口。不运行lua项目,而是运行C++就能看到输出日志。然后编译出新的模拟器后再运行lua项目就能看到日志了。

   

 

2.lua语法

--[[Lua语法注意事项:1.function前一定要加local2.函数必要要在调用代码的前面3.定义每个变量,前面都要加local,在方法中加local就是方法内的局部变量,在方法外加local就是文件级别的全局变量--]]local function printTest()    print(type(""))    print(type(100))    print(type(100.0))    print(type(true))    print(type(print))    print(type(nil))    print(type( { x = 10, y = 20 }))endlocal function funTest()    local s = 123    print(tostring(s))    local r = true    if (s == 1213) then        print(tonumber(r))    else        print("nonono")    endendlocal function funFor()    for i = 1, 5 do        print(i)    endendlocal function funWhile()    local i = 0    while i < 5 do        print(i)        i = i + 1    endendlocal function funRepeat()    local i = 0    repeat        i = i + 1        print("Repeat " .. i)    until (i > 5)endlocal function funForIn()    local arr = { 2, 3, 4, 1, 45, 321, 221 }    for key, var in ipairs(arr) do        print(key .. ":" .. var)    endend--表变量local function funObj()    local obj = { id = 1, name = "zhangsan" }    print(obj.id)    print(obj.name)    for key, var in ipairs(obj) do        print(key .. ":" .. var)    endendlocal global = 1local function funGlobal()    local local1 = 3    global = global + 1    return global, local1end-- 函数内嵌套函数local function calculate(opr, a, b)    local function add(a, b)        return a + b    end    local function sub(a, b)        return a - b    end    local result    if opr == "+" then        result = add(a, b)    else        result = sub(a, b)    end    return resultend-- 返回函数local function rectangleArea(width, height)    local area = width * height    return areaendlocal function triangleArea(bottom, height)    local area = 0.5 * bottom * height    return areaendlocal function getArea(type)    local returnFunction    if type == "rect" then        --        returnFunction = rectangleArea        -- 匿名函数        returnFunction = function(width, height)            local area = width * height            return area        end    else        --        returnFunction = triangleArea        -- 匿名函数        returnFunction = function(bottom, height)            local area = 0.5 * bottom * height            return area        end    end    return returnFunctionend--Student = { id = 100, name = "Tony" }--local function Student.toString()--    local s = "Name:" .. self.name .. " id:" .. self.id--    return s--endlocal function init()    cc.exports.MY_GLOBAL = "hello"    funTest()    printTest()    funFor()    funWhile()    funRepeat()    funForIn()    funObj()    local i1, i2 = funGlobal()    print(i1 .. ' .. ' .. i2)    print("global" .. global)    if (local1 == nil) then        print("local1 is nil")    else        print("local1" .. local1)    end    local res1 = calculate("+", 10, 5)    print("10+5=" .. res1)    local res2 = calculate("-", 10, 5)    print("10-5=" .. res2)    local area = getArea("tria")    print("底10高13,三角形面积:" .. area(10, 15))    local area = getArea("rect")    print("宽10高15,计算长方形面积:" .. area(10, 15))--    print(Student.toString())endinit()return 1

 

3.通过引擎创建lua类

local LuaTestClass = class("LuaTestClass")cc.updataDir="cc.updataDir"LuaTestClass.a=1LuaTestClass.b=2function LuaTestClass.funTest2()    print("LuaTestClass.funTest2")endfunction LuaTestClass:funTest3()    print("LuaTestClass.funTest3")    self.funTest2() -- 通过self执行类的其他方法endfunction LuaTestClass:tostring()   local s= "Name=" .. self.a .. " id=" .. self.b   return sendreturn LuaTestClass

 

调用:

local testClass = require("app.LuaTestClass")    print("cc.updataDir2" .. cc.updataDir)    testClass.funTest2()    testClass:funTest3()   local s=testClass:tostring()   print(s)

 

4.给lua项目添加cjson库

(1) cjson所在目录

 

(2) 引用cjson库

 

(3) 添加调用代码

 

(4) lua代码中调用 csjon 库

以上代码没有调用成功,待续。。。

 

转载于:https://www.cnblogs.com/zhuawang/p/6649295.html

你可能感兴趣的文章
Java 虚拟机:互斥同步、锁优化及synchronized和volatile
查看>>
2.python的基本数据类型
查看>>
python学习笔记-day10-01-【 类的扩展: 重写父类,新式类与经典的区别】
查看>>
查看端口被占用情况
查看>>
浅谈css(块级元素、行级元素、盒子模型)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
PHP开源搜索引擎
查看>>
12-FileZilla-响应:550 Permission denied
查看>>
ASP.NET MVC 3 扩展生成 HTML 的 Input 元素
查看>>
LeetCode 234. Palindrome Linked List
查看>>
编译HBase1.0.0-cdh5.4.2版本
查看>>
结构体指针
查看>>
迭代器
查看>>
Food HDU - 4292 (结点容量 拆点) Dinic
查看>>
Ubuntu安装Sun JDK及如何设置默认java JDK
查看>>
[经典算法] 排列组合-N元素集合的M元素子集
查看>>
Codeforces 279D The Minimum Number of Variables 状压dp
查看>>
打分排序系统漫谈2 - 点赞量?点赞率?! 置信区间!
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>