Button Lua-检查光标是否位于按钮内部
所以基本上我写了这些代码来检查我的光标是否在一个按钮的内部,也就是mousover。它工作得很完美,但是我真的不喜欢我写if语句的方式Button Lua-检查光标是否位于按钮内部,button,lua,position,Button,Lua,Position,所以基本上我写了这些代码来检查我的光标是否在一个按钮的内部,也就是mousover。它工作得很完美,但是我真的不喜欢我写if语句的方式 --cursor = table containing x and y value of cursor --self = table containing x and y value of button --self.W = returns width of button --self.H = return h
--cursor = table containing x and y value of cursor
--self = table containing x and y value of button
--self.W = returns width of button
--self.H = return height of button
function mousover(cursor)
if cursor.x >= self.x --if cursor is inside of button from left side
and cursor.x <= self.x + self.W --if cursor is inside of button from right side
and cursor.y >= self.y --if cursor is inside of button from top side
and cursor.y <= self.y + self.H then --if cursor is inside of button from bottom side
doSomething()
end
--cursor=包含光标x和y值的表
--self=包含按钮x和y值的表格
--self.W=返回按钮的宽度
--self.H=按钮的返回高度
功能鼠标悬停(光标)
if cursor.x>=self.x——若光标位于按钮左侧的内部
和cursor.x=self.y——若光标位于按钮顶部的内部
和cursor.y正如我最初在评论中所说:
没有更好的办法了
这是一个传统的边界框复选框。边界框有四条边,因此需要检查四个条件
想想代码在做什么,为什么要这样做,然后你应该意识到它确实需要做它当前正在做的一切。代码对我来说似乎很好(可能将其抽象为一个函数),但既然这样做有效,它就应该打开。我怀疑这是否“更好”。它的代码可能更少,但可读性较差,性能也较差。
function mousover(cursor)
if math.floor((cursor.x - self.x)/self.W)^2 +
math.floor((cursor.y - self.y)/self.H)^2 == 0 then
-- DoSomething()
end
end