Как запустить функцию щелчка мышью только один раз - PullRequest
0 голосов
/ 05 апреля 2020

Решено .. LUA как зафиксировать щелчок мыши, чтобы свет не быстро мигал. Мне пришлось создать latch, latch = 0, а затем в функции lights () сравнить, когда latch и ms [3] были разными или одинаковыми, и что делать в каждом случае.

lToggle = {cs=0,x1=1,x2=40,y1=1,y2=40}
ms = {}
latch = 0
function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    if numDecimalPlaces ~= nil then
        return math.floor(num * mult + 0.5) / mult
    else
        return math.floor((num * mult + 0.5) / mult)
    end
end
function msState(v)
    return {round(v.getMouseX() * 100,1),round(v.getMouseY() * 100,1),v.getMouseState()}
end

function lights()
ms = msState(screen)
  if ms[3] == 1 and latch == 0
    then
    latch = 1
  elseif ms[3] == 0 and latch == 1
    then
      latch = 0
      if ms[1] >= lToggle.x1 and ms[1] <= lToggle.x2 and ms[2] >= lToggle.y1 and ms[2] <= lToggle.y2
        then
          light.toggle()
      else end
  else end
end

function html()
    screen.setHTML([[<html><body>
    <style>
    #test {
    position: absolute;
    top: 50%;
    left: 50%;
    font-size: 40px;
    color: green;
    }
#test2 {
    position: absolute;
    top: 20%;
    left: 20%;
    width: 30%;
    height: 30%;
    font-size: 40px;
    color: green;
    border: 5px solid red;
    }
    </style>
    <span id="test">Test</span>
        <span id="test2"> X:]].. ms[1] ..[[  </br>Y:]].. ms[2]..[[</br>Click State:]].. ms[3] ..[[</span>
         <!--   <span id="test2"></span>-->
    </body></html>]])

    end

Тогда в system.update () есть:

screen.clear()
lights()
html()

1 Ответ

0 голосов
/ 06 апреля 2020

Этот вопрос решен. Я добавил защелку = 0 в начало и изменил это:

    function lights()
ms = msState(screen)
  if ms[3] == 1
    then
   if ms[1] >= lToggle.x1 and ms[1] <= lToggle.x2 and ms[2] >= lToggle.y1 and ms[2] <= lToggle.y2
        then
          light.toggle()
      else end
  else end
end

На:

function lights()
ms = msState(screen)
  if ms[3] == 1 and latch == 0
    then
    latch = 1
  elseif ms[3] == 0 and latch == 1
    then
      latch = 0
      if ms[1] >= lToggle.x1 and ms[1] <= lToggle.x2 and ms[2] >= lToggle.y1 and ms[2] <= lToggle.y2
        then
          light.toggle()
      else end
  else end
end
...