2013-02-14 36 views
-1

我工作的應用看你跑得多快,爲此,我需要一個函數,顯示你的最高速度已。但無法找到我的工作方式。如何獲得lua中的最大數值?

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設爲上面顯示的數字。

我Conora SDK/Lua的

+0

我不能你已瞭解代碼。你想比較多個「速度」對象嗎?你能給我們一個函數頭嗎? – Makah 2013-02-14 12:32:59

回答

2

你應該給出更多的信息,當你問堆棧溢出的問題,但讓我們盡力幫助你反正程序。

你的代碼可能是一個事件監聽器,看起來像這裏面:

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 

顯示當前速度。如果你想顯示的最高速度,而不是,只是做這樣的事情:

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 

的想法是:你需要定義監聽器(或全局)變量來存儲先前的最高速度。每次調用事件監聽器時,如果當前速度高於先前的最大速度,則它是新的最大速度,因此您可以保存並顯示它。