zoukankan      html  css  js  c++  java
  • cocos2d-x学习笔记

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

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

       

    2.lua语法

    --[[
    
    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

    调用:

    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 库

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

  • 相关阅读:
    How to convert VirtualBox vdi to KVM qcow2
    (OK)(OK) adb -s emulator-5554 shell
    (OK)(OK) using adb with a NAT'ed VM
    (OK) How to access a NAT guest from host with VirtualBox
    (OK) Creating manually one VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK)(OK) Creating VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK) Creating_VMs_from_an_existing_VDI_file.txt
    (OK) Creating VMs from an existing VDI file —— in OS X
    (OK) install_IBM_SERVER.txt
    (OK) install chrome & busybox in android-x86_64 —— uninstall chrome
  • 原文地址:https://www.cnblogs.com/zhuawang/p/6649295.html
Copyright © 2011-2022 走看看