如何获得lua的最大值? [英] how can i get a maximum number value in lua?

查看:718
本文介绍了如何获得lua的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究应用程序,以观察您的运行速度,为此,我需要一个函数来显示您的最大速度.但找不到我的方法.

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 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

我已将speedText设置为您在上面看到的数字.

I've made my speedText to a number that you see above.

我在Conora SDK/Lua中编程

I program in Conora SDK/Lua

推荐答案

当您问有关Stack Overflow的问题时,您应该提供更多信息,但是无论如何,我们还是尝试为您提供帮助.

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.

这篇关于如何获得lua的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆