--这是Lua的语句
print("hello lua")
--[[
这个是多行注释的区域
Lua变量类型
1.数字类型
2.字符串类型
3.thread类型 线程
4.function 函数
5.table 唯一的数组融合了链表和键值对
6.其他类型
]]
--把多行注释两个横杆去掉,就变成多行文本输出
text=[[
chang wen ben
heng jian dan
]]
print(text)
a = function( var )
print("user input is "..var)
end
a("I'm A")
--table唯一使用大括号的地方
myTable = {
12,
13
}
--输出table的方法
for k,v in pairs(myTable) do
print(k,v)
end
--[[
输出的结果
1 12
2 13
]]
--如果一个函数function函数中创建的变量也是global(没有使用local关键字),那么也意味着它是全局变量
b = "123" --是全局类型,其他文件引用
local c = 12 --是一个private类型,不能被其他文件访问到
--[[
Lua中的条件判断语句
1 条件判断的特殊符号组合 == > < >= <= ~=不等于
2没有()then elseif连一起 也没有switch语句
]]
local a =12
if a == 12 then
print("a's value is 12")
end
if a==11 then
print("value is 11")
elseif a ~= 11 then --!=是~=
print("value is not 11")
end
--完整的 if else 语句块
if a==2 then
print("value is 2")
elseif a==11 then
print("value is 11")
else
print("value is "..a)
end
--[[
Lua中的函数
]]
--无参函数
local function myfunc01()
print("im myfunc01")
end
--调用函数 无参数的
myfunc01()
--有参函数
local function myfunc02(a)
print (a)
end
myfunc02("im ok")
--有参数 也有返回值的参数
local function myfunc03(a)
return a
end
print(myfunc03("im im"))
--可以返回多返回值,并接收 Lua的特性
local function myfunc04()
return 1,2
end
local a,b = myfunc04()
print(a)
print(b)
--函数里面可以传递 参数...可变
local function myfunc05(...)
local mtable = {...}
--遍历
for k,v in pairs(mtable) do
print(k,v)
end
end
myfunc05("12",123,"ok")
--[[myfunc05输出的结果 table是从1开始mtable[1]
1 12
2 123
3 ok
]]
--[[
Lua中的循环语句
while语句
do while lua中是repeat until
for语句
在lua中没有加加操作符
]]
--while
mtable={1,2,3}
local index=1
while mtable[index] do
print(mtable[index])
index = index +1
end
--repeat until
local snum=1
repeat
print("snum value is "..snum)
snum = snum + 1
until snum == 4
--[[输出结果为
snum value is 1
snum value is 2
snum value is 3
]]
--for语句
for i=1,#mtable do --#mtable取长度
print(mtable[i])
end
for i=1,10,2 do --i=1,10,2从1到10 步进是2
print(i)
end
--[[输出的结果
1 下面20
3 18
5 16
7 ...
9 2
]]
for i=20,1,-2 do
print(i)
end
--[[
Lua中的逻辑运算符
and or not
and 如果第一个我们需要去计算操作数,如果是假的话,那么就返回第一个操作数,反之返回第二个操作数。
or 如果第一个我们需要去计算操作数,如果是真的话,那么就返回第一个操作数,反之返回第二个操作数。
not 永远返回的只是true和false
lua中只有false nil为假 0不是为假
]]
print(1 and 5)
print(0 and 5)
print(false and 5)
print(nil and 5)
--[[输出结果
5
5
false
nil
]]
print(1 or 5)
print(0 or 5)
print(false or 5)
print(nil or 5)
--[[输出结果
1
0
5
5
]]
print(not nil)--不是假返回true 输出结果true
print(not false) --输出结果true
print(not 0)--不是真返回false 输出false
print(not 1) --输出false
--[[
Lua中的table 类似于list和map的结合体
1当配置文件 配置集 阵列
2当数字集 <xml\jason> 发包时,注意加密
3Lua中的table索引不是0 它是1!!!
4table访问的方式跟我们在其他语言中数组的方式很像
5Lua中的三种遍历方式
6区分for pairs for ipairs
7作为第三方插件集成到项目中,为项目提供一个支持功能
8也可完全使用lua进行开发,quick-lua CoronaSDK
]]
--建立一个空table
mytable = {}
--建立一个有数据table
mytable2 = {
2,
3,
4,
6,
"ok",
config = {1,2,3}, --数组中包含数组,但不输出
k = 12,--没有输出
8
}
for i=1,#mytable2 do
print(mytable2[i])
end
mytable3 = {
k = 12 --k为索引 不是连续数字
}
print(mytable3[k])--输出nil
print(mytable3["k"])--输出12
print(mytable3.k)--输出也是12 lua中mytable.k == mytable["k"] {}花括号里面k就是键值对
--a.x a[x]区别 a.x == a["x"] a[x] == 以变量x的值来索引table
--第一种遍历方式
for i=1,#mytable2 do
print("value is ==>"..mytable2[i])
end
--第二种遍历方式for ipairs ipairs迭代器使用的方式跟我们第一种普通for的值一样的,都是按照隐士的索引值来显示
for i,v in ipairs(mytable2) do
print(i,v)
end
--[[输出的结果
1 2
...
5 ok
6 8
]]
--第三种遍历方式for pairs迭代器把所有值输 并且table中索引 并不是按照完全顺序来的。
for k,v in pairs(mytable2) do
print(k,v)
end
--[[输出结果
1 2
...
5 ok
6 8
k 12
config table: 004C96A8
]]
print("---------------------------------")
--[[
Lua应用
]]
--当配置集 配置信息
application_config = {
game_config = {
isDebugModel = false,
isCheatModel = true
},
sound_config = {
isBackgroundMusciOpen = true,
isEffectsOpen = false
},
texture_config = {
Plist_Directory = "res/plist",
packer_texture_director = "res/images"
}
}
--当阵列使用enemy_datas.lua
enemy_waves = {
{enterId = 1,infoid = 1},
{enterId = 2,infoid = 1},
{enterId = 2,infoid = 2},
{enterId = 1,infoid = 1},
{enterId = 1,infoid = 1}
}
enemy_datas = {
{maxHp = 100,damage = 100,isHaveSpecialEffect = true },
{maxHp = 100,damage = 100,isHaveSpecialEffect = true },
{maxHp = 100,damage = 100,isHaveSpecialEffect = true }
}
for k,v in pairs(enemy_datas) do
print(k,v)
end
--[[输出结果
1 table: 00410410
2 table: 004103C0
3 table: 00410438
]]
for k,v in pairs(enemy_datas) do
if type(v) == "table" then
for ik,iv in pairs(v) do
print(ik,iv)
end
print("===================")
end
end
--[[输出结果
damage 100
isHaveSpecialEffect true
maxHp 100
===================
damage 100
isHaveSpecialEffect true
maxHp 100
===================
damage 100
isHaveSpecialEffect true
maxHp 100
===================
]]
LUA中读取文件的方法
--[[
Lua中 读写外部的数据 使用自带的io库
io.open("路径名",'r') r表示读取的权限(read) a表示追加(append) w表示write b表示打开二进制binary
]]
--read.Lua
local file = assert(io.open("namelist.txt",'r'))--assert断言 路径名和读写
local string = file:read("*all")--*all表示读取所有文件的内容 *line 读取一行 *number读取一个数字 <num>读取一个不超过<num>字符
file:close() --close关闭流
print(string)
--封装一下
local function read_files(fileName)
local file = assert(io.open(fileName,'r'))
local content = file:read("*all")
file:close()
return content
end
--调用读取函数
print(read_files("namelist.txt"))
--write.Lua中
local file =assert(io.open("ok.txt",'w'))
file:write("welcome to lua\n 欢迎您的到来")
file:close()
--封装写入的函数
local function write_files(fileName,content)
local file = assert(io.open(fileName,'w'))
file:write(content)
file:close()
end
local long_string = [[
土地是以它的肥沃和收获而被估价的;才能也是土地,不过它生产的不是粮食,而是真理。如果只能滋生瞑想和幻想的话,即使再大的才能也只是砂地或盐池,那上面连小草也长不出来的。 —— 别林斯基
]]
write_files("ok.txt",long_string)
LUA中的串行化
--[[
Lua中的串行化Serialiazition
序列化数据,将数据转化为字流
]]
function serialize(o)
if type(o) == "number" then
io.write(o)
elseif type(o) == "string" then
--io.write("'",o,"'")
io.write("[[",o,"]]")
end
end
serialize("ok")
serialize(12)
--string.format("%q",a)
a= 'a "programing lua"\\"'
print(string.format("%q",a))
--[[输出结果
"a \"programing lua\"\\\""
]]
--保存无环的table
function new_serialize(o)
if type(o) == "number" then
io.write(o)
elseif type(o) == "string" then
io.write(string.format("%q",o))
elseif type(o) == "table" then
io.write("{\n")
for k,v in pairs(o) do
io.write(" ",k,"=")
new_serialize(v)
io.write(",\n")
end
io.write("}\n")
else
end
end
new_serialize{a=12,b="lua"}
--[[输出结果
{
a=12,
b="lua",
}
]]
LUA中Require和模块module编写方法
--[[
1.require的使用方式 系统库已经隐士的创建好了,不需要重复的创建
2.module模块编写方法。用来模拟面向对象的编写,也就是table的使用。
require用途
1.判断这个包是否存在
2.判断包是否加载
3.如果没有 返回nil或者报错
4.反之则返回相应的模块对象
module(...)函数相当于
]]
require("aa") --或者require "aa"
showName("ok")
--aa.showName("ok")还没有编写模块
--文件夹app/test/cc.lua
--requrie("app.test.cc")--写点就可以自动加载a_b.lua == a.b
local tt = require("mymodule")
tt.showName()
--mymodule.lua
--创建一个模块
complex = {
}
function complex.showName()--面向对象的伪
print("LOL -- lolxcc")
end
return complex
--aa.lua
function showName(v)
print("yangjianyong"..v)
end
--这是Lua的语句
print("hello lua")
--[[
这个是多行注释的区域
Lua变量类型
1.数字类型
2.字符串类型
3.thread类型 线程
4.function 函数
5.table 唯一的数组融合了链表和键值对
6.其他类型
]]
--把多行注释两个横杆去掉,就变成多行文本输出
text=[[
chang wen ben
heng jian dan
]]
print(text)
a = function( var )
print("user input is "..var)
end
a("I'm A")
--table唯一使用大括号的地方
myTable = {
12,
13
}
--输出table的方法
for k,v in pairs(myTable) do
print(k,v)
end
--[[
输出的结果
1 12
2 13
]]
--如果一个函数function函数中创建的变量也是global(没有使用local关键字),那么也意味着它是全局变量
b = "123" --是全局类型,其他文件引用
local c = 12 --是一个private类型,不能被其他文件访问到