목적없이 남의 코드를 분석하는 것은 굉장히 지루하게 느껴질 때도 있지만 그래도 성취감은 있는 것 같습니다. 물론 긴 코드는 엄두도 못내지만요.. 100~150줄 정도 되는 코드는 해볼만 하군요~
어쨌든 마우스를 올렸을 때 색이 바뀌고, 클릭했을 때 텍스트가 바뀌도록 루아로 코딩을 해 보았습니다.
메타테이블을 이용해서 프로토타입(클래스)을 만들었구요, 프로토타입에서 프로토타입 바깥의 함수를 호출하도록 코딩한 것이 중요점입니다.
예제실행: http://lotusjk77.com/wp-content/game/mousehover/index.html
mouseHover
lotusjk77.com
아래 코드에는 공부를 위해 수정하지 않은 코드가 포함되어 있습니다.
gnrRect = {t="btn", x=0,y=0,sizeX=100,sizeY=100,color="rgba(0,255,255,1)";
clickEvent=function() print("notYet") end,
new = function(self,o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
update = function(self)
stb=self.t
self.bgcolor = string.gsub(self.color, "1%)","0.5)") -- 마지막 1은 투명도이다.
local maxX = self.x + self.sizeX/2
local minX = self.x - self.sizeX/2
local maxY = self.y + self.sizeY/2
local minY = self.y - self.sizeY/2
if mouse.x<maxX and mouse.x>minX
and mouse.y<maxY and mouse.y>minY then
self.isHover = 1
else
self.isHover = nil
end
if self.isHover and mouse.pressed == 1 then
print(self.t)
self.clickEvent()
else
end
end,
draw = function(self)
local color
if self.isHover then
color = self.bgcolor
else
color = self.color
end
screen:fillRect(self.x, self.y, self.sizeX, self.sizeY, color)
screen:drawText(self.t, self.x,self.y,20,"rgb(255,255,255)")
end,
}
init = function()
R1 = gnrRect:new({t="A btn",x=-75,y=-42,sizeX=90,sizeY=30,
color ="rgba(255,0,0,1)",clickEvent=function() R1event() end})
R2 = gnrRect:new({t="B btn",x=75,y=42,sizeX=90,sizeY=30,
color ="rgba(0,255,0,1)",clickEvent=function() R1event() end})
R3 = gnrRect:new({t="C btn",x=0,y=0,sizeX=90,sizeY=30,
color ="rgba(0,0,255,1)",clickEvent=function() R1event() end})
clickT="click"
end
update = function()
R1:update()
R2:update()
R3:update()
end
draw = function()
screen:clear()
R1:draw()
R2:draw()
R3:draw()
screen:drawText(clickT,-100,50,30,"rgb(255,255,255)")
function R1event()
clickT= stb
end
R2event = function()
clickT= stb
end
R3event = function(a)
clickT= "C btn"
end
end
'루아(LUA) > 마이크로스튜디오_예제' 카테고리의 다른 글
클릭할 때마다 배경색 바뀌는 예제 (0) | 2022.01.13 |
---|---|
1초마다 랜덤으로 색이 바뀌는 배경 (0) | 2022.01.13 |
랜덤으로 숫자 생성하기 (0) | 2022.01.10 |
루아로 만드는 초시계 (0) | 2022.01.10 |
마우스를 따라다니는 원의 충돌 현상 구현 (0) | 2022.01.10 |