사용자 도구

사이트 도구


language:corona:lua

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
language:corona:lua [2017/04/06 19:49] kieunslanguage:corona:lua [2024/04/23 22:44] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +====== 언어 퀵 요약 ======
  
 +==== 일반 ====
 +
 +<code lua>
 +-- 한줄 주석
 +
 +--[[
 +블록 주석
 +]]
 +
 +-- NULL
 +local v_nil = nil
 +
 +-- 변수
 +foo = 3
 +
 +-- 함수 호출
 +print(foo)
 +
 +print("테스트");
 +-- ';'을 붙여도 되고 안붙여도 되고
 +
 +v1 = 10 -- 
 +local v2 = 12 -- local
 +print("v1,v2: " .. v1,v2) -- 프린트 함수는 무한 입력?
 +
 +a = "Lua"
 +do
 + print("1: " .. a)
 + local b = "Zua"
 + c = "ActionScript"
 +end
 +print("2: " .. a)
 +print("3: " .. tostring(b))
 +print("4: " .. c)
 +
 +
 +print(type("HelloWorld"))
 +print(type(10.4 * 3))
 +print(type(print))
 +
 +--[[ type
 + - nil
 + - true, false
 + - string
 + - number
 +]]
 +
 +--[[ 비교 연산자
 + > , < >=, <= , ==, 
 + ~= (같지않다.)
 +]]
 +
 +--[[ 논리 연산자
 + and, or, not
 +]]
 +
 +--[[ 그외 연산자
 + 문자열연결 : "ㅁ" .. "B"
 + 테이블문자열 길이 변환: #
 +]]
 +
 +-- if 을 사용하려면
 +math.randomseed(os.time()) -- 어쩌다 한번 해주는게 좋지
 +local rnd = math.random(1,100)
 +if rnd % 2 == 0 then
 + print(rnd .. ": 짝")
 +else
 + print(rnd .. ": 홀")
 +end
 +
 +-- 반복문 for
 +for i = 10, 1, -1 do -- 1, 10, 1 == 변수, 조건, 증감연산자
 + print(i)
 +end
 +
 +-- 반복문 while (선검사 후실행)
 +local i2 = 1
 +while i2 <= 5 do
 + print(i2)
 + i2 = i2 + 1
 +end
 +
 +-- 반복문 repeat (선실행 후검사)
 +local i3 = true
 +repeat
 + local rnd = math.random(1,100)
 + print("CONT: " .. rnd)
 + if rnd < 50 then 
 + i3 = false
 + end
 +until i3 == false
 +
 +-------------------------------------------------------------------------------
 +
 +-- 함수
 +function add1(a,b) 
 + return a+b;
 +end
 +-- 함수 : local, global 있다
 +local function add2(a,b)
 + return a+b
 +end
 +-- 함수 : 변수에 할당 가능
 +local fn_add3 = function(a, b)
 + return a^b
 +end
 +print(fn_add3(10, 2))
 +-- 함수 두개 이상 값 돌려줄 수 있다. 각자 타입이 달라도 된다.
 +
 +
 +-------------------------------------------------------------------------------
 +-- 모듈1
 +
 +-- string 모듈
 +print(string.byte("A")) -- to 65
 +print(string.char(65))
 +print(string.find("Hello Corona App", "Coro")) -- 7, 10 -- 시작과 끝을 알아낸다
 +-- gmatch(string, "format") : 문자열에서 포맷과 일치되는 것을 찾는다
 +-- string.gsub(스트링, "찾는스트링", "바꿀스트링")
 +-- string.gsub(스트링, "찾는스트링", "바꿀스트링", 몇번째것을 바꿀까?)
 +
 +-- math
 +-- os
 +--os.exit() -- 종료
 +
 +-- 프로퍼티와 메소드에 접근할 때 
 +-- . 연산자를 이용합니다. 
 +-- Lua의 경우 프로퍼티는 . 로 접근하지만 메소드는 : 을 이용합니다.
 +
 +-------------------------------------------------------------------------------
 +-- 모듈2
 +
 +local ControlMyCar = require "ControlMyCar"
 +local car1 = ... -- '...' 이거 뭐지?
 +ControlMyCar.connect(car1)
 +ControlMyCar.lockDoors(false)
 +
 +
 +-------------------------------------------------------------------------------
 +-- 코루틴
 +
 +-- 코루틴은 쓰레드가 아니다. 아래 코드를 실행하면 fn1()만 무한 실행된다.
 +--[[
 +local _b = true
 +local function fn1()
 + while _b do
 + print("OK")
 + end
 + print("END")
 +end
 +local function fn2()
 + _b = false
 +end
 +local co1 = coroutine.create(fn1)
 +local co2 = coroutine.create(fn2)
 +coroutine.resume(co1)
 +coroutine.resume(co2)
 +]]
 +
 +-- yield로 현재루틴을 잠시 중지하고 다른 코루틴이 실행 되도록 한다. 
 +local _b = true
 +local function fn1()
 + while _b do
 + print("OK")
 + coroutine.yield()
 + end
 + print("END")
 +end
 +local function fn2()
 + _b = false
 +end
 +local co1 = coroutine.create(fn1)
 +local co2 = coroutine.create(fn2)
 +coroutine.resume(co1)
 +coroutine.resume(co2)
 +-- yield는 실행을 그 지점에서 종료하는 것, 뒤부분을 실행하려면 resume()를 추가로 호출해야한다.
 +coroutine.resume(co1)
 +
 +-------------------------------------------------------------------------------
 +-- 테이블: 배열,해시,어레이의 짬뽕
 +
 +local _table = {}
 +_table[1] = 1
 +_table["five"] = 5
 +print(_table[1] + _table["five"])
 +
 +-- 숫자, 문자, 함수, 모듈, 객체 모두 저장가능
 +local function temp_fn_1()
 + print("Did you call me?")
 +end
 +local _tbl2 = {a=3, b="String", c=temp_fn_1, d=math}
 +print(tostring(_tbl2.d.random()))
 +print(_tbl2.c() )
 +--print(_tbl2.c() .. _tbl2.d.random())
 +
 +local _tbl3 = {3,1,100,5,7,9}
 +-- table.concat() 요소연결
 +print(table.concat(_tbl3,'--'))
 +-- table.copy() 테이블 합치기
 +-- table.indexOf()
 +table.sort(_tbl3)
 +print(table.concat(_tbl3,'--'))
 +-- table.remove
 +table.remove(_tbl3, 2)
 +print(table.concat(_tbl3,'--'))
 +--print(table.indexOf(_tbl3, 2)) indexOf는 사라진듯?
 +
 +-------------------------------------------------------------------------------
 +
 +-- API
 +--[[
 +tostring()
 +tonumber()
 +pairs() 테이블을 key, value 형태로 되돌려주는 함수
 +ipairs() 키가 숫자로 된것만 리턴하는 
 +]]
 +
 +</code>
 +
 +==== 커스텀모듈 ====
 +
 +<code lua>
 +-- 별도 파일을 만들어서,
 +
 +-- 이렇게 시작
 +local Class = {}
 +
 +Class.car = nil
 +
 +Class.connect = function(car)
 +Class.car = car
 +print("Class.Connect")
 +end
 +
 +Class.lockDoors = function(isLock)
 +print("Class.lockDoors")
 +end
 +
 +Class.ignition = function(isOn)
 +print("Class.ignition")
 +end
 +
 +Class.move = function(isForward, speed)
 +print("Class.move")
 +end
 +
 +Class.handling = function(isLeft, degree)
 +print("Class.handling")
 +end
 +
 +return Class
 +</code>
 +
 +쓸때는
 +
 +<code lua>
 +local ControlMyCar = require("ControlMyCar")
 +-- ControlMyCar.lua 파일 있음
 +local car = ...
 +ControlMyCar.connect(car)
 +</code>
 +
 +다른 폴더 밑에 두었다면, my_custom 폴더에 있다면
 +
 +<code lua>
 +local ControlMyCar = require "my_custom.ControlMyCar"
 +</code>