how can i get a maximum number value in lua?

前端 未结 1 998
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 09:17

I am working on app to watch how fast you run, and for that I need a function that shows what your maximum speed has been. but can not find how I do.

local speed         


        
相关标签:
1条回答
  • 2021-01-29 09:36

    you should give more information when you ask questions on Stack Overflow, but let's try to help you anyway.

    You code is probably inside an event listener that looks like that:

    local listener = function(event)
      local speedText = string.format( '%.3f', event.speed )
      speed.y = 250
      speed.x = 125
      local numValue = tonumber(speedText)*3.6
      if numValue ~= nil then
          speed.text = math.round( numValue )
      end
    end
    

    This displays the current speed. If you want to display the maximum speed instead, just do something like this:

    local maxSpeed = 0
    local listener = function(event)
      local speedText = string.format( '%.3f', event.speed )
      speed.y = 250
      speed.x = 125
      local numValue = tonumber(speedText)*3.6 or 0
      if numValue > maxSpeed then
          maxSpeed = numValue
          speed.text = math.round( numValue )
      end
    end
    

    The idea is: you need a variable defined outside the listener (or a global) to store the previous maximum speed. Every time the event listener is called, if the current speed is higher than the previous maximum speed, then it is the new maximum speed so you save it and you display it.

    0 讨论(0)
提交回复
热议问题