In Deadly Lava, you learned how to trigger code based on user behavior. This tutorial shows you how to make a platform which fades away when a user steps on it.
設定
If you followed Deadly Lava, you can place your fading platform above the lava floor that users can't jump across.
插入一個部件並將其移動到您世界中的合適位置。將其命名為 FadingPlatform。
調整大小以便用戶可以跳上去。
確保它是 固定的。

在部件中插入一個 腳本,將其重新命名為 FadeOnTouch,並刪除默認代碼。

為平台創建一個變數,並連接到平台的 Touched 事件的一個空函數。
local platform = script.Parentlocal function fade()endplatform.Touched:Connect(fade)
減少透明度的平台
讓平台瞬間消失會讓用戶難以穿越這個間隙,因此平台應該在用戶掉下之前 逐漸消失 以給他們機會跳下去。
您可以改變 Transparency 屬性並反复等待很短的時間來達成這種效果,但逐漸消失至少需要在0到1之間改變10次。這將會是20行非常重複的代碼。
這可以通過使用一個 for 循環來更有效地完成,該循環可以重複代碼特定次數。每次循環代碼被稱為 迭代。for 循環由三個部分定義,並用逗號分隔:

- 控制變數 - 創建並用於計算循環的變數。在這個示例中,它是 count,起始值為1。
- 結束值 - 它必須達到的值才能停止循環。在這個示例中,它是10。
- 步進增量 (可選) - 確定每次循環要添加到控制變數的值。如果省略,則默認為1,因此在這個示例中不需要。
在 FadeOnTouch:
在函數中,創建一個從 1 開始的 for 循環,它迭代 10 次。
在 for 循環內,將 Transparency 屬性設置為控制變數除以 10。
使用 task.wait() 函數等待 0.1 的時間。
local platform = script.Parentlocal function fade()for count = 1, 10 doplatform.Transparency = count / 10task.wait(0.1)endendplatform.Touched:Connect(fade)
當循環運行時,count 在每次迭代時增加1。這意味著平台的 Transparency 每0.1秒增加0.1,1秒後達到完全透明。
再次出現
在平台消失後,用戶應該會從其下方掉落。平台在消失後幾秒內應該會再出現,否則用戶如果失敗就無法再次嘗試跳躍。CanCollide 屬性控制用戶是否可以穿過一個部件。
在 for 循環之後將平台的 CanCollide 屬性設置為 false。
使用 task.wait() 函數等待幾秒的時間。
將 CanCollide 屬性設置回 true。
將 Transparency 屬性設置回 0。
local platform = script.Parentlocal function fade()for count = 1, 10 doplatform.Transparency = count / 10task.wait(0.1)endplatform.CanCollide = falsetask.wait(3)platform.CanCollide = trueplatform.Transparency = 0endplatform.Touched:Connect(fade)
去抖動變數
In Deadly Lava, you learned that the Touched event runs each time a user's body part comes into contact with the part. This behavior causes issues when a user runs across the fading platform: the function will run multiple times, resetting the loop each time.
For the code to work properly, the function should only run once when the user touches the platform for the first time. Ensuring that an action is only triggered once when it would otherwise be triggered multiple times is known as 去抖動。
要去抖動一個函數,可以使用一個 boolean 變數來跟蹤平台是否已被觸碰。布林值只能包含 true 和 false 的值。創建一個名為 isTouched 的變數並將其設置為 false。
local platform = script.Parent
local isTouched = false
local function fade()
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
end
platform.Touched:Connect(fade)
檢查變數
可以使用 if 語句僅在 isTouched 去抖動變數為 false 時運行淡入函數中的代碼。將淡入函數的主體包裹在一個條件為 not isTouched 的 if 語句中。
local platform = script.Parent
local isTouched = false
local function fade()
if not isTouched then
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
end
end
platform.Touched:Connect(fade)
Luau 的 not 運算符會反轉其後的值。在條件上,這意味著第一個 if 語句的行為與其後的語句相同。
if not isTouched thenendif isTouched == false thenendif isTouched == nil thenend
切換去抖動
目前,fade 函數中的代碼將始終運行,因為 isTouched 是 false 且 not isTouched 的結果為 true。要完成去抖動的程序,您需要在兩個地方切換該變數的值。
- 在 if 語句內,將 isTouched 設置為 true,在平台開始淡入之前。
- 在平台重新出現後將其設置為 false。
local function fade()
if not isTouched then
isTouched = true
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
isTouched = false
end
end
platform.Touched:Connect(fade)
And that's it! Test your code now, and you should find that the platform fades away when the user jumps on it and comes back a few seconds later.
You can duplicate this platform across a wider gap to create a challenging obstacle and change the speed at which they fade to balance the difficulty.
最終代碼
local platform = script.Parent
local isTouched = false
local function fade()
if not isTouched then
isTouched = true
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
isTouched = false
end
end
platform.Touched:Connect(fade)