콘텐츠로 이동

Event - 이벤트 핸들링

이벤트 등록하기

MouseButton1Click 혹은 GetPropertyChangedSignal 같은 로블 기본 이벤트를 간단하게 등록하고 싶으신가요? Event 를 이용해보세요.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
local ScreenGUI = script.Parent
local Quad = require(path.to.module).Init()
local Class = Quad.Class
local Mount = Quad.Mount
local Event = Quad.Event
local Store = Quad.Store

local TextButton = Class "TextButton"
local Count = 0

TextButton "mainButton" {
    Size = UDim2.fromOffset(200,200);
    Text = "0 번 클릭했습니다";
    [Event "MouseButton1Click"] = function(self,x,y)
        Count = Count + 1
        self.Text = Count .. " 번 클릭했습니다"
    end;
}
Mount(ScreenGUI, Store.GetObject("mainButton"))

이밴트 리스트

적용가능한 Event 들은 다음과 같습니다

Event(name:string)

모든 로블록스 이벤트들을 나타냅니다, MouseEnter, MouseLeave, MouseButton1Down, MouseButton1Up 등이 될 수 있습니다
기본적으로 로블록스의 기본 이벤트들과 다르게, 첫번째 인자로 self 를 받을 수 있습니다.

1
2
3
4
5
Frame {
    [Event "MouseEnter"] = function (self)
        print(self.Name .. "에 마우스가 올라왔습니다")
    end;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
local ScreenGUI = script.Parent
local Quad = require(path.to.module).Init()
local Class = Quad.Class
local Mount = Quad.Mount
local TextButton = Class "TextButton"

TextButton "mainButton" {
    Size = UDim2.fromOffset(200,200);
    [Event "MouseEnter"] = function (self)
        print("버튼이 클릭되었습니다")
    end;
}
Mount(ScreenGUI, Store.GetObject("mainButton"))

Event.Created

오브젝트가 생성되었을 때 실행됩니다. 첫번째 인자로 self 를 받을 수 있습니다.

1
2
3
4
5
6
-- 용법
Frame {
    [Event.Created] = function (self)
        print(self.Name .. "이 생성되었어요")
    end;
}

주의

Event.Created 가 실행될 때는 Parent 값이 정해지지 않았을 수 있습니다. self.Parent 는 접근해서는 안됩니다. Class.Extend 에서는 AfterRender 를 이용해야 합니다.


Event.CreatedAsync

Event.Created 와 비슷하지만, 코루틴 안에서 작동합니다. objectList:EachAsync() 처럼 반환을 기다리지 않습니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-- 용법
Frame {
    Frame {
        Name = "A";
        [Event.CreatedAsync] = function (self)
            -- 이 함수가 끝나지 않더라도 다음 오브젝트가 생성될 수 있어야합니다
            task.wait(5)
        end;
    };
    Frame {
        Name = "B";
        [Event.Created] = function (self)
            print(self.Name .. "가 생성되었습니다")
        end;
    };
}

주의

Event.CreatedAsync 가 실행될 때는 Parent 값이 정해지지 않았을 수 있습니다. self.Parent 는 접근해서는 안됩니다.


Event.Prop(propertyName:string)

프로퍼티 값이 변경됨을 연결합니다.

1
2
3
4
5
6
7
8
-- 용법
TextBox {
    Text = "아무거나 입력하세요";
    [Event.Prop "Text"] = function (self,text)
        -- 편의상의 이유로 변경된 값이 제공됩니다
        print(text)
    end;
}