计时器和事件

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

在回合进行期间,脚本需要跟踪时间并在不同的脚本之间发送信号。时间将使用时间脚本进行管理,而事件,是 Roblox 编程中的一个概念,将显示更改,例如比匹配结束。

用事件发送信号

现在玩家已经在竞技场中,事件可以用来显示比赛开始的信号,计时器代码也可以开始。之后,事件也可以用来显示比匹配结束,以及是时候将玩家转回大厅了。

这些事件不是预制的,因此需要创建自定义事件对象称为 可绑定事件 。可绑定事件经常用于玩家发起的行动,与事件如 TouchedChanged 类似。

多个脚本可以监听相同的绑定事件。这使您的代码井然有序,以便以后需要时更容易添加比赛的开始或结束部分的额外代码。

创建可绑定的事件

首先创建可绑定的事件对象来匹配的开始和结束。因为可绑定的事件不会与客户端互动,所以它们可以存储在服务器存储中。

  1. 在 ServerStorage 中,创建一个名为 Events 的新文件夹。在该文件夹中,创建两个 可绑定事件 名为 MatchStart 和 MatchEnd 的文件。

使用事件

现在,当玩家进入竞技场时,中场休息继续重启而不是开始计时器。主游戏循环需要被告知停止,等待匹配结束事件发生,然后才能移至代验证码的下一部分。

事件有两个内置函数:Connect()Wait()。而不是使用之前的 Connect() ,在 MatchEnd 结束时调用 Wait() 以暂停游戏管理脚本,直到 MatchEnd 发射。在这种情况下,等待函数暂停代码,直到游戏管理器收到信号称比赛结束了。

  1. 游戏管理器 中,为 Events 文件夹和 MatchEnd 事件创建变量。


    -- 模块脚本
    local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
    local matchManager = require(moduleScripts:WaitForChild("MatchManager"))
    local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
    -- 事件
    local events = ServerStorage:WaitForChild("Events")
    local matchEnd = events:WaitForChild("MatchEnd")
  2. 让脚本等待结束事件发射,然后继续移动。在 循环 中,在 结束 时,键输入: matchEnd.Event:Wait()


    while true do
    repeat
    task.wait(gameSettings.intermissionDuration)
    print("Restarting intermission")
    until #Players:GetPlayers() >= gameSettings.minimumPlayers
    print("Intermission over")
    task.wait(gameSettings.transitionTime)
    matchManager.prepareGame()
    -- 等待器放置为游戏的长度。
    matchEnd.Event:Wait()
    end
  3. 测试 游戏。确认一旦玩家进入竞技场,中场循环 不会继续 。脚本现在正在等待 matchEnd 信号发触发。

排除问题的提示

在这一点上,代码没有按期望的方式工作,请尝试以下方法之一。

  • matchEnd.Event:Wait() 中重复检查 dot 或 colon 运营商的使用。
  • 确保匹配结束是可绑定的事件,而不是另一种输入,例如远程事件。

使用计时器

导致比赛结束的条件之一是计时器到期,这将通过脚本处理。

设置计时器

要将计时器添加到游戏中,请在下面的步骤中使用预制模块脚本。它包括启动和结束计时器的功能,还包括返回剩余时间的功能。

  1. 在服务器存储 > 模块脚本中,创建一个名为计时器的新模块脚本。

    将代码替换为以下代码。


    local Timer = {}
    Timer.__index = Timer
    function Timer.new()
    local self = setmetatable({}, Timer)
    self._finishedEvent = Instance.new("BindableEvent")
    self.finished = self._finishedEvent.Event
    self._running = false
    self._startTime = nil
    self._duration = nil
    return self
    end
    function Timer:start(duration)
    if not self._running then
    task.spawn(function()
    self._running = true
    self._duration = duration
    self._startTime = tick()
    while self._running and tick() - self._startTime < duration do
    task.wait()
    end
    local completed = self._running
    self._running = false
    self._startTime = nil
    self._duration = nil
    self._finishedEvent:Fire(completed)
    end)
    else
    warn("Warning: timer could not start again as it is already running.")
    end
    end
    function Timer:getTimeLeft()
    if self._running then
    local now = tick()
    local timeLeft = self._startTime + self._duration - now
    if timeLeft < 0 then
    timeLeft = 0
    end
    return timeLeft
    else
    warn("Warning: could not get remaining time, timer is not running.")
    end
    end
    function Timer:isRunning()
    return self._running
    end
    function Timer:stop()
    self._running = false
    end
    return Timer
  2. 在匹配管理器中,需要游戏设置和计时器模块。


    local MatchManager = {}
    -- 服务
    local ServerStorage = game:GetService("ServerStorage")
    -- 模块脚本
    local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
    local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
    local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
    local timer = require(moduleScripts:WaitForChild("Timer"))
  3. 在变量下面,通过设置一个名为 myTimer 的变量等于 timer.new() 来创建一个新的计时对象。这个对象将用于调用启动和停止计时器的函数。


    local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
    local timer = require(moduleScripts:WaitForChild("Timer"))
    -- 创建一个新的计时对象,用于跟踪比赛时间。
    local myTimer = timer.new()

开始和停止

现在创建了计时器,在比匹配期间使用包含的函数 start()stop() 。以下是每个函数的描述和它接受的参数。

  • start(time) - 启动计时器,参数为秒数时间。
  • finished:Connect(functionName) - 当计时器结束时,运行传递作为参数的函数。
  1. 匹配管理器 中,创建一个名为 timeUp() 的新函数来运行计时器结束时。包括测试打印声明。


    local myTimer = timer.new()
    -- 本地函数
    local function timeUp()
    print("Time is up!")
    end
    -- 模块功能
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    end
    return MatchManager
  2. timeUp() 下,添加一个名为 startTimer() 的函数,带有打印声明。你将在游戏中稍后显示计时器。


    -- 本地函数
    local function timeUp()
    print("Time is up!")
    end
    local function startTimer()
    print("Timer started")
    end
  3. 要启动和停止计时器,在 startTimer() :

    • 调用 myTimer.start() . 传递 gameSettings.matchDuration .
    • 调用 myTimer.finished:Connect() . 传递 timeUp() .

    -- 本地函数
    local function startTimer()
    print("Timer started")
    myTimer:start(gameSettings.matchDuration)
    myTimer.finished:Connect(timeUp)
    end

启动计时器

计时器可以使用匹配开始事件在比赛开始时触发。

  1. 在匹配管理器中,在模块变量下,创建变量来存储事件文件夹、匹配开始和匹配结束(这在未来的课程中使用)。


    -- 模块脚本
    local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
    local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
    local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
    local timer = require(moduleScripts:WaitForChild("Timer"))
    -- 事件
    local events = ServerStorage:WaitForChild("Events")
    local matchStart = events:WaitForChild("MatchStart")
    local matchEnd = events:WaitForChild("MatchEnd")
    --创建计时器
    local myTimer = timer.new()
  2. return MatchManager 上,连接匹配开始事件到 startTimer()


    -- 模块功能
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    end
    matchStart.Event:Connect(startTimer)
    return MatchManager
  3. 要发射匹配开始事件,在 prepareGame() 中,输入 matchStart:Fire() .


    -- 模块功能
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    matchStart:Fire()
    end
  4. 测试游戏。在输出窗口中,确认您可以看到计时器启动和停止功能的打印声明。

已完成的脚本

以下是完成的脚本来检查您的工作。

匹配管理器脚本


local MatchManager = {}
-- 服务
local ServerStorage = game:GetService("ServerStorage")
-- 模块脚本
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
local timer = require(moduleScripts:WaitForChild("Timer"))
-- 事件
local events = ServerStorage:WaitForChild("Events")
local matchStart = events:WaitForChild("MatchStart")
local matchEnd = events:WaitForChild("MatchEnd")
-- 创建一个新的计时对象,用于跟踪比赛时间。
local myTimer = timer.new()
-- 本地函数
local function timeUp()
print("Time is up!")
end
local function startTimer()
print("Timer started")
myTimer:start(gameSettings.matchDuration)
myTimer.finished:Connect(timeUp)
end
-- 模块功能
function MatchManager.prepareGame()
playerManager.sendPlayersToMatch()
matchStart:Fire()
end
matchStart.Event:Connect(startTimer)
return MatchManager

游戏管理器脚本


-- 服务
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
-- 模块脚本
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local matchManager = require(moduleScripts:WaitForChild("MatchManager"))
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
-- 事件
local events = ServerStorage:WaitForChild("Events")
local matchEnd = events:WaitForChild("MatchEnd")
while true do
repeat
task.wait(gameSettings.intermissionDuration)
print("Restarting intermission")
until #Players:GetPlayers() >= gameSettings.minimumPlayers
print("Intermission over")
task.wait(gameSettings.transitionTime)
matchManager.prepareGame()
-- 等待器放置为游戏的长度。
matchEnd.Event:Wait()
end