简体   繁体   English

Lua脚本的倒数计时器

[英]Countdown timer for Lua script

I am completely new to the Lua programming language, I've only been working with it a few days in total. 我对Lua编程语言是完全陌生的,总共才使用了几天。 Although, I do have some experience in Python, C# and Ada. 虽然,我确实有一些Python,C#和Ada的经验。

I am currently trying to make a racing game, one of the aspects of the game I'm trying to include is a countdown timer that ends the game after 90 seconds, then returning to a high score screen, regardless of whether or not the player has eliminated all of their lives already. 我目前正在尝试制作一款赛车游戏,我想包含的游戏内容之一是倒数计时器,该计时器在90秒后结束游戏,然后返回高分屏幕,无论玩家是否已经消灭了他们所有的生命。 I haven't a clue what functions to include nor where the place the text in the main script. 我不知道要包括哪些功能,也不知道文本在主脚本中的位置。 Some advice would be much appreciated. 一些建议将不胜感激。

When the game starts do: 游戏开始时,请执行以下操作:

local startTime = os.time()
local endTime = startTime+90

Then regularely do: 然后定期执行以下操作:

if os.time() >= endTime then
    -- exit game
    -- return to high score screen
end

until the game ends. 直到游戏结束。 Probably this would be in a callback function that gets called regularely. 可能是在经常调用的回调函数中。 Depending on how your framework works, a loop might also work. 根据框架的工作方式,循环也可能起作用。

Use timer.performWithDelay to call a specified function after a delay. 延迟后,使用timer.performWithDelay调用指定的函数。

Example

local function countdown( event )
    print( "listener called" )
end

timer.performWithDelay( 1000, countdown, 90 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM