161217--lua學習 代碼筆記--基礎篇1

–這是一個lua語句

print("hello lua")
a =
--[[
    <html>
    <body>
    <a> http://www.baidu.com</a>
    </body>
    </html>
    這之間是一個多行註釋
]]

print(a)

條件語句

a=2
if a == 12 then
    print(a)
else
    print("nil")
end

注意

~=   --不等於

函數

local function myfun01( ... )
    -- body
    print("ia function01")
end
myfun01();
local function myfunc02()
    -- body
    return 1,2;
end

local b,c= myfunc02();
print (b);
print(c);

循環

function pair( ... )
    -- body
    local mytable = {...}
    for k,v in pairs(mytable) do
        print(k,v)
    end
end
pair(12,"eqw")

********************************************
– and or not
– and 如果第一個我們要操作的數是假的話,那麼我們會返回第一個操作數,否則則返回第二個操作數;
–lua中只有false和nil爲假 0也是真;

print (0 and 1)
print(false and 2)
print(1 and 5)

–or 如果我們計算的第一個值時真的時候返回第一個,假的時候返回第二個;

print (1 or 2)
print(false or 3)

–not 返回的只有true 和false

--while
m_table = {1,2,3}
local index = 1;
while m_table[index] do
    print(m_table[index])
    index = index+1
end
while index<10 do
    print (index)
    index = index +1
end
--repeat
local s=1
repeat
    s = s+1
    print (s)
until s==3

–for

for i=0,10,3 do
    print(i)
end

*********************************************
–table
–lua中table的索引1 不是0
–table的訪問和其他語言中的數據使用類似

mytable1 = {1,2,3,4}
for i=1,#mytable1 do  --#代表時表的長度
print()
end

mytable2 = {1,2,table3={4,5},8}

***************

s = "ok"
mytable3 = {
    k=12
}
mytable3[s]= 12
print(table["k"])
print(table[s])
print(table.k)
--在lua中 table.k等價於table["k"]

-- a.x  a[x]
-- a.x  等價於 a["x"]
--a[x]  以變量x的值來索引table

–第一種遍歷

 for u=1,#table3 do
    print("value is==>"..mytable3[i])
 end

–第二種 for ipairs 迭代器使用的方式跟我們第一種普通for循環獲取的值時一樣的;
–都是按照當前的索引 迭代並顯示,不包括鍵值對

 for i,v in ipairs(mytable3) do
    print(i,v)
 end

–第三種 for pairs 全部都是顯示,包括鍵值對
–完全將所有的值顯示出來,並且要注意table中的索引並不完全時按照書寫順序來的

 for k,v in pairs(mytable3) do
    print(k,v)
 end

**********
–lua 在實際的應用中可以作爲第三方插件集成到項目中,爲我們的項目提供一個支持功能;
–可以完全使用lua進行開發,quick-cocos2dx,coronaSDK
–當作一種數據的配置集(陣列)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章