創建一個硬幣收集機制

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


現在您已經擁有了 3D 世界,教學的這一部分教您如何添加第一個腳本來定義收集硬幣的機制。這個機制讓玩家收集硬幣,並禁用最近收集的硬幣的收集。

創建金幣

在你能寫任何東西之前,你需要在世界上擁有假物體來使用它們作為你的硬幣。像上一節中你製作的海堆平台一樣,金幣可以是簡單的 Part 對象。

要創建硬幣:

  1. 檢索器 窗口中,新增一個新文件夾到 世界 文件夾中,然後重命名為 金幣

  2. 筒體 零件插入 金幣 文件夾,然後將零件重命名為 金幣

    Studio's Explorer window with the Coin part highlighted. The hierarchy of the Workspace to World folder to Coins folder to Coin part is also highlighted.
  3. 選擇零件,然後在 屬性 視窗中,

    • 磚塊顏色 設置為 金色
    • 材料 設為 金屬
    • 尺寸 設為 0.6, 8, 4
    • 停用 CanCollide 。這會告訴引擎其他零件可以通過硬幣,意味著玩家可以通過硬幣來收集它們。
    • 啟用 錨定 。這會告訴引擎永遠不要因為任何物理相關模擬而改變硬幣的位置,這意味著玩家可以觸碰硬幣而不會影響其位置。
    A close up view of a gold coin next to two gray cylinder sea stacks on the island.
  4. 複製一些額外的硬幣,並將它們放置在地圖上進行測試。

    Studio's Explorer window with multiple Coin parts highlighted under the Coins folder. A view of multiple coins on the island and two gray cylinder sea stacks.

你的圓筒零件現在看起來像硬幣,並防止物理模擬,但你需要將邏輯添加到硬幣上,以便玩家可以收集它們。

創建腳指令碼

為了讓硬幣能被收集,你想要回應玩家觸碰它們。Roblox 引擎可以通知您當某些東西觸碰到硬幣時,但您需要在指令碼本中宣言這一點。要創建一個指令碼:

  1. 檢索器 窗口中,將鼠標懸停在 ServerScriptService 上,然後單擊 按鈕。一個上下文菜單顯示。

  2. 從上下文選單中,選擇 腳本 。新的腳本會顯示在 服務器腳本服務 下,告訴引擎在服務伺服器上執行腳本,並防止客戶端存取代碼。

    Studio's Explorer window with both ServerScriptService's plus icon and Script object highlighted.
  3. 將腳本重命名為 CoinService

    Studio's Explorer window with the CoinService script highlighted under ServerScriptService.
  4. 將預設代碼替換為以下代碼:


    -- 初始化服務和變量
    local Workspace = game:GetService("Workspace")
    local Players = game:GetService("Players")
    local coinsFolder = Workspace.World.Coins
    local coins = coinsFolder:GetChildren()
    local COOLDOWN = 10
    -- 定義事件處理器
    local function onCoinTouched(otherPart, coin)
    if coin:GetAttribute("Enabled") then
    local character = otherPart.Parent
    local player = Players:GetPlayerFromCharacter(character)
    if player then
    -- 玩家觸碰了一枚硬幣
    coin.Transparency = 1
    coin:SetAttribute("Enabled", false)
    print("Player collected coin")
    task.wait(COOLDOWN)
    coin.Transparency = 0
    coin:SetAttribute("Enabled", true)
    end
    end
    end
    -- 設定事件聆聽器
    for _, coin in coins do
    coin:SetAttribute("Enabled", true)
    coin.Touched:Connect(function(otherPart)
    onCoinTouched(otherPart, coin)
    end)
    end

    現在,每當玩家觸碰硬幣時,硬幣將消失 10 秒,輸出日誌打印 Player collected coin

    以下部分詳細說明腳本如何運作。

    初始化服務和變量

    和你可能在其他語言寫過的大量代碼一樣,你在腳指令碼頂部定義需要稍後使用的變量。我們的代碼會做到以追蹤中事情:

    • 取得服務實例 - Roblox 服務提供內置功能以支持常見功能。腳本首先獲得 Workspace 服務的實例,該服務包含 3D 世界中的每個對象,以及 Player 服務,該服務管理和包含連接到您體驗的所有玩家。

    • 獲得所有硬幣的參考 - 腳本然後查詢3D工作區中所有與GetChildren()相關的硬幣對象的參考。這個方法返回包含與對應的對象相關的所有內容的數組,在這個情況下是你之前創建的 Workspace.World.Coins 文件夾。

    • 定義一個全球變量 - COOLDOWN 變量稍後使用來定義在收集後禁用硬幣的時間。

      初始化服務和變量

      local Workspace = game:GetService("Workspace")
      local Players = game:GetService("Players")
      local coinsFolder = Workspace.World.Coins
      local coins = coinsFolder:GetChildren()
      local COOLDOWN = 10
      ...

    定義事件處理器

    Roblox 引擎物理模擬 3D 世界,並處理大量與渲染、物理和網路相關的事件的邏輯。當您對某些事件感興趣時編寫自己的邏輯時,您可以聆聽並處理它們,同時讓引擎完成剩下的部分。在這種情況下,您聆聽並處理與金幣接觸相關的事件。該腳本在 onCoinTouched() 方法中定義處理此事件的邏輯,進行以追蹤中操作:

    • 偵測是否啟用金幣 - 每個 Instance 都有一個 Enabled 二元參數,定義是否存在 3D 世界中的對象。您可以使用 GetAttribute() 方法獲得實例屬性。

    • 偵測玩家是否觸碰了金幣 - 如果啟用了金幣,方法會使用玩家服務來檢查觸碰金幣的對象是否真的是玩家。當觸摸事件發生時,Roblox 引擎會將觸碰硬幣的對象作為 otherPart 參數傳送。腳本檢查是否父親 otherPart 屬於玩家。

    • 如果玩家觸碰了硬幣,就會停用硬幣;在 10 秒後重新啟用硬幣 - 最後,如果玩家觸碰了硬幣,方法會等待 10 秒,然後重新啟用硬幣以進行收收藏。task.wait() 被用來取代wait() ,因為它不會完全暫停代碼執行,允許其他線程的任務並行運行。

      定義事件處理器

      local function onCoinTouched(otherPart, coin)
      if coin:GetAttribute("Enabled") then
      local character = otherPart.Parent
      local player = Players:GetPlayerFromCharacter(character)
      if player then
      -- 玩家觸碰了一枚硬幣
      coin.Transparency = 1
      coin:SetAttribute("Enabled", false)
      print("Player collected coin")
      task.wait(COOLDOWN)
      coin.Transparency = 0
      coin:SetAttribute("Enabled", true)
      end
      end
      end

    連接事件處理器

    所有從 BasePart 模擬的 3D 物件繼承了事件 Touched() ,因此具有一個事件。下列循環將 onTouchedEvent() 處理器連接到每個硬幣的觸發事件,進行以追蹤中操作:

    • 循環通過所有金幣 - 使用一般循環循環通過每個金幣。

    • 將處理器連接到事件 - 在循環的每一次迭代中,硬幣默認啟用,因此在體驗初始啟動期間在 3D 世界中可見。onCoinTouched() 處理器方法也連接到硬幣的Touched事件,以便每次發生事件時運行。當引擎偵測到觸碰時,它也會傳送觸碰對象的物件,otherPart

      連接事件處理器

      for _, coin in coins do
      coin:SetAttribute("Enabled", true)
      coin.Touched:Connect(function(otherPart)
      onCoinTouched(otherPart, coin)
      end)
      end

玩測機制

是時候看看硬幣收集機制是否如預期般運作了。要測試您的體驗:

  1. 在工具欄中,單擊 播放 按鈕。Studio 進入播放測試模式。

    Play button highlighted in Studio's playtesting options.
  2. 將角色移動到觸碰硬幣。如果您的腳本正確運行, 輸出 窗口會顯示Player collected coin,並在重新出現前消失10秒的時間。

    Studio's Output window that displays confirmation that the player collected a coin.