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

COCOS2D-X 快速熟悉LUA细节问题

主要快速讲了一些Lua的特性,所以快速学习Lua的话,建议先看一遍。

一、{},构造式

{a = 1, b= 2} 等价于 {["a"] = 1,["b"] = 2}

{"x","y","z"} 等价于 {[1] = "x",[2] = "y", [3] = "z"}

 


如果真的需要以0为数组的话:

a = {[0] = "x","y","z"}  -- 不推荐这样

 


二、尽量使用局部变量,便于垃圾收集器释放内存

如果是全局变量,局部使用的时候,有时可以考虑:

local temA = A

 


使用局部变量保存全局变量的值,如果后面函数会改变A的值,可以考虑这样,也可以加快当前域的访问速度。

 

 

 

三、控制语句

 


1、if then else

 


2、while


[cpp
while true or false do 
-- todo 
end 

while true or false do
-- todo
end

 

3、repeat-until


[cpp]
repeat 
-- todo 
until true or false 

repeat
-- todo
until true or false

 

4、for

 


for 起始,变量, 步长(不写默认1) do

--todo

end

 


5、iterator


[cpp]
for k,v in ipairs(a) do 
-- k是索引 
-- v是对应的value 
end 
 
for k in pairs(a) do 
-- k是索引 
end 

for k,v in ipairs(a) do
-- k是索引
-- v是对应的value
end

for k in pairs(a) do
-- k是索引
end


可以用来逆向table:


[cpp]
revA = {} 
for k,v in ipairs(a) do 
revA[v] = k 
end 

revA = {}
for k,v in ipairs(a) do
revA[v] = k
end

 

注意:其他的switch、do-while 没有

 


五、return、break ,只能放在一个块的结束语句(end、else、until)前面


[cpp]
function func() 
local x = 0 
x = 1 
print(x) 
return  -- 想在这里跳出,这样是错误的。 
if(x>1) then 
x = 10 
end 
end  

function func()
local x = 0
x = 1
print(x)
return  -- 想在这里跳出,这样是错误的。
if(x>1) then
x = 10
end
end


修改成:


[cpp]
function func() 
local x = 0 
x = 1 
print(x) 
do 
return -- 这样才是正确的,可以执行return 
end 
if(x>1) then 
x = 10 
end 
end  

function func()
local x = 0
x = 1
print(x)
do
return -- 这样才是正确的,可以执行return
end
if(x>1) then
x = 10
end
end

 

六、function,函数可以有多个返回值

例如:


[cpp]
function func()  
return "a","b" --返回2个值 
end 

function func()
return "a","b" --返回2个值
end

 

 


1、如果函数是表达式的最后一个,则函数保留尽可能多的返回值用来赋值变量


[cpp]
x = func()      -- x = "a",返回值"b"不要 
x,y = func()        -- x = "a", y = "b" 
x,y,z = 1,func()     -- x = 1, y = "a" , z = "b"  注意func是表达式的最后一个 
x,y,z = func()          -- x = "a", y = "b" , z = nil  返回值不够用nil补充 

x = func()   -- x = "a",返回值"b"不要
x,y = func()    -- x = "a", y = "b"
x,y,z = 1,func()     -- x = 1, y = "a" , z = "b"  注意func是表达式的最后一个
x,y,z = func()       -- x = "a", y = "b" , z = nil  返回值不够用nil补充

 

 


2、如果函数不是表达式的最后一个,则只返回一个值


[cpp]
x,y,z = func(),1    -- x = "a", y = 1, z = nil   

x,y,z = func(),1 -- x = "a", y = 1, z = nil 

 

3、函数在table构造式中,如果函数在构造式的最后一个位置,返回所有值,否则返回一个值


[cpp]
t = {func()} --t = {"a","b"} 
t = {func(),2} --t = {"a",2} 

t = {func()} --t = {"a","b"}
t = {func(),2} --t = {"a",2}


4、函数在return之后,返回所有值

 


5、如果函数在()中,则只返回一个值


[cpp]
print(func()) --输出 a b 
print((func())) --函数被放在了()中,输出 a 

print(func()) --输出 a b
print((func())) --函数被放在了()中,输出 a

 

6、函数实参可以是变长参数,如果函数实参同时包含了固定参数和变长参数,变长参数必须放在固定参数后面。请注意…的用法

 


七、如何写带描述的函数实参


[cpp]
//OC代码:  
- (void) createRect:(float)X y:(float)Y width:(float)Width height:(float)Height 

//TODO...  

//OC代码:
- (void) createRect:(float)X y:(float)Y width:(float)Width height:(float)Height
{
//TODO...
}

 

有时候函数参数很多,想用oc这种带描述的参数。lua虽然不支持,但是可以用table实现。


[cpp]
function createRect(rectOptions) 
if type(rectOptions.title) ~= "string" then 
error "need title" 
end 
end 
 
createRect({x = 0,y = 0 , width = 200,height = 300 , title = "hello sunny"}) 

function createRect(rectOptions)
if type(rectOptions.title) ~= "string" then
error "need title"
end
end

createRect({x = 0,y = 0 , width = 200,height = 300 , title = "hello sunny"})

 

提示:函数实参只有一个table时候,可以不要(),所以可以写成:


[cpp]
createRect{x = 0,y = 0 , width = 200,height = 300 , title = "hello sunny"} 

createRect{x = 0,y = 0 , width = 200,height = 300 , title = "hello sunny"}

 

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