我正在尝试通过教程来学习如何在 Roblox 中制作游戏。我在网站上的部分:指南、教程、环境、游戏内声音。然后在页面底部一直显示“播放声音”。这是它说的:
为 SoundService 创建一个变量,然后创建另一个变量来存储反馈声音。
local pickupObjects = game.Workspace.Collectables.Objects
local objectsArray = pickupObjects:GetChildren()
local SoundService = game:GetService("SoundService")
local feedbackSound = SoundService:FindFirstChild("FeedbackSound")
local function partTouched(otherPart, objectPart)
然后下一部分说:
要播放铃声,请找到功能
partTouched
。在 if
语句中,调用 feedbackSound:Play()
来播放声音。
local function partTouched(otherPart, objectPart)
local whichCharacter = otherPart.Parent
local humanoid = whichCharacter:FindFirstChildWhichIsA("Humanoid")
-- Play the sound, once finished, destroy the object
if humanoid and objectPart.CanCollide == true then
feedbackSound:Play()
end
end
我不知道创建变量是什么意思。它没有给我一个。我应该输入什么内容,在哪里输入? 下一部分我不知道
call feedbackSound:Play()
是什么意思,call
是什么意思,我该怎么办?
在 StarterPlayer > StarterPlayerScripts 中,创建一个名为 CollectableSounds 的新本地脚本。
只要玩家触摸收藏品,下面的代码就会运行partTouched函数。将代码复制到您的脚本中。
然后这是他们提供的脚本:
local pickupObjects = game.Workspace.Collectables.Objects
local objectsArray = pickupObjects:GetChildren()
local function partTouched(otherPart, objectPart)
local whichCharacter = otherPart.Parent
local humanoid = whichCharacter:FindFirstChildWhichIsA("Humanoid")
if humanoid and objectPart.CanCollide == true then
end
end
-- Binds every object part to the touch function so it works on all parts
for objectIndex = 1, #objectsArray do
local objectPart = objectsArray[objectIndex]
objectPart.Touched:Connect(function(otherPart)
partTouched(otherPart, objectPart)
end)
end
我输入:
local SoundService = game:GetService("SoundService")
local feedbackSound = SoundService:FindFirstChild("FeedbackSound")
在较大脚本的第 2 行和第 4 行之间
然后我输入:
feedbackSound:Play()
较大脚本的第 9 行
我尝试玩测试游戏,走进绿色宝石时没有声音。 我注意到
feedbackSound:Play()
括号内没有任何内容,因此我尝试将声音 ID 放入其中 rbxassetid://4110925712
(带或不带括号)。 我尝试将 FeedbackSound
放在括号内。
首先,以问题的名义回答问题,“调用”函数意味着“运行”它:
function ExampleFunction(Value) -- "define" the function
local NewValue = Value + 10
print(tostring(Value) .. " plus 10 is " .. tostring(NewValue))
end
ExampleFunction(5) -- "call" the function
在这个例子中,我:
在 Roblox 中,内部有多种类型的数据存储功能。让我们以 FindFirstChild 为例,因为它似乎是问题的中心:
InstanceA:FindFirstChild("InstanceB")
此行将在 InstanceA 中搜索名为“InstanceB”的实例,如果不存在这样的实例,则这将“返回”一个称为 nil 的值,这是 Luau 表示什么都没有的方式。然后可以像您一样将该值存储在变量中:
local feedbackSound = SoundService:FindFirstChild("FeedbackSound")
此行将在 SoundService 实例中搜索名为“FeedbackSound”的实例。非常重要的是,这个 Sound 实例实际上存在于 SoundService 内部,因为您可以像稍后在脚本中那样对待它。
现在,我无法访问您的位置,所以我只能假设您的问题是:
rbxassetid://4110925712
。如果未设置声音的 SoundId,则播放它不会发生任何事情。
:Play()
函数不接受声音ID,事实上,括号中没有任何值。所以这条线实际上就很好。 (如果在括号中输入任何内容,将会出现错误)
== true
语句时写“
if
”是一个常见的错误,哎呀,我以前也这样做过!然而,这是完全没有必要的。它所做的就是将
CanCollide
的值与
true
进行比较,如果是
true
,则结果为
true
,这是多余的。