cocos2d-x學習筆記

1.新建的lua項目使用print()函數沒法打印日誌的問題json

   解決:是由於lua項目用的是babelua插件,自己不能輸出日誌到窗口。不運行lua項目,而是運行C++就能看到輸出日誌。而後編譯出新的模擬器後再運行lua項目就能看到日誌了。babel

   

 

2.lua語法app

--[[

Lua語法注意事項:
1.function前必定要加local
2.函數必要要在調用代碼的前面
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 }))
end

local function funTest()
    local s = 123
    print(tostring(s))

    local r = true
    if (s == 1213) then
        print(tonumber(r))
    else
        print("nonono")
    end
end

local function funFor()
    for i = 1, 5 do
        print(i)
    end
end

local function funWhile()
    local i = 0
    while i < 5 do
        print(i)
        i = i + 1
    end
end

local function funRepeat()
    local i = 0
    repeat
        i = i + 1
        print("Repeat " .. i)
    until (i > 5)
end

local function funForIn()
    local arr = { 2, 3, 4, 1, 45, 321, 221 }
    for key, var in ipairs(arr) do
        print(key .. ":" .. var)
    end
end

--表變量
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)
    end
end

local global = 1
local function funGlobal()
    local local1 = 3
    global = global + 1
    return global, local1
end

-- 函數內嵌套函數
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 result
end

-- 返回函數
local function rectangleArea(width, height)
    local area = width * height
    return area
end

local function triangleArea(bottom, height)
    local area = 0.5 * bottom * height
    return area
end

local 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 returnFunction
end


--Student = { id = 100, name = "Tony" }
--local function Student.toString()
--    local s = "Name:" .. self.name .. " id:" .. self.id
--    return s
--end





local 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())
end

init()

return 1

 

3.經過引擎建立lua類函數

local LuaTestClass = class("LuaTestClass")

cc.updataDir="cc.updataDir"

LuaTestClass.a=1
LuaTestClass.b=2

function LuaTestClass.funTest2()
    print("LuaTestClass.funTest2")
end

function LuaTestClass:funTest3()
    print("LuaTestClass.funTest3")
    self.funTest2() -- 經過self執行類的其餘方法
end

function LuaTestClass:tostring()
   local s= "Name=" .. self.a .. " id=" .. self.b
   return s
end

return LuaTestClass

 

調用:ui

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

   local s=testClass:tostring()
   print(s)

 

4.給lua項目添加cjson庫lua

(1) cjson所在目錄spa

 

(2) 引用cjson庫插件

 

(3) 添加調用代碼日誌

 

(4) lua代碼中調用 csjon 庫code

以上代碼沒有調用成功,待續。。。

相關文章
相關標籤/搜索