작은이야기
2009. 10. 23. 12:41
사용자 입력처리는 Scene에서 처리 한다.
class 'testaScene' (Scene)
function testaScene:__init(id) super(id)
self:SetupUIObjects()
self:SetMsgHandler(Evt_KeyboardDown, self.OnKeyDown)
self.playerNumber = 0
end
function testaScene:OnKeyDown(msg)
local move = 3
local dir = Vector2(0, 0)
local pressedL, optL = self:GetKeyPressed(UIConst.KeyLeft)
local pressedU, optU = self:GetKeyPressed(UIConst.KeyUp)
local pressedR, optR = self:GetKeyPressed(UIConst.KeyRight)
local pressedD, optD = self:GetKeyPressed(UIConst.KeyDown)
if pressedL then dir.x = dir.x-move end
if pressedR then dir.x = dir.x+move end
if pressedU then dir.y = dir.y-move end
if pressedD then dir.y = dir.y+move end
--local keyValue = msg:GetValue(Evt_KeyboardDown.key.Value)
--if keyValue == UIConst.KeyUp then dir.y = dir.y - move end
--if keyValue == UIConst.KeyDown then dir.y = dir.y + move end
--if keyValue == UIConst.KeyLeft then dir.x = dir.x - move end
--if keyValue == UIConst.KeyRight then dir.x = dir.x + move end
local msgChat = Message(Evt_Move)
msgChat:SetValue(Evt_Move.key.id, self.playerNumber)
msgChat:SetValue(Evt_Move.key.x, dir.x)
msgChat:SetValue(Evt_Move.key.y, dir.y)
gameapp:SendToServer(msgChat)
end |
방향키의 입력에 따라서 서버로 패킷을 보내는 함수다.
이벤트를
function testaScene:__init(id) super(id) 안에 등록 해줘야 한다.
방향키 입력 받는 걸 두가지 방식으로 생각해봤다.
self:GetKeyPressed()
msg:GetValue(Evt_KeyboardDown.key.Value)
이 두가지 함수가 있다.
첫번째 것은 다른 이벤트에서도 사용할 수 있다.
동시에 여러개의 키가 눌려있는지 검사하므로 대각선도 인식이 된다.
두번째 것은 키다운 이벤트에서만 사용 가능하다.
동시에 여러개의 키가 눌려있는지 검출이 안된다.
정의된 프로토콜을 이용해서 패킷을 서버로 보낸다.