Roblox Studio 中的 Laua 中的 call 是什么意思?

问题描述 投票:0回答:1

我正在尝试通过教程来学习如何在 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)

然后下一部分说:

要播放铃声,请找到功能部分Touched。在 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 放在括号内。

lua roblox luau
1个回答
0
投票

首先,以问题的名义回答问题,“调用”函数意味着“运行”它:

function ExampleFunction(Value) -- "define" the function
    local NewValue = Value + 10
    print(tostring(Value) .. " plus 10 is " .. tostring(NewValue))
end

ExampleFunction(5) -- "call" the function

在这个例子中,我:

  • 定义一个名为“ExampleFunction”的函数,它接受一个值,并告诉您该值加 10 是多少。
  • 在括号内,我写了一个变量名称“Value”(当然,如果我愿意,我可以添加更多,用逗号分隔),这些正式称为“参数”。这些是函数“接收”的值的名称。
  • 在最后一行,我通过提供函数名称并后跟括号来运行该函数。 这称为 “调用” 函数
  • 在括号内,我输入了函数将接受的值(同样,我可以输入更多值,用逗号分隔),正式称为 “参数”

在 Roblox 中,内部有多种类型的数据存储功能。让我们以 FindFirstChild 为例,因为它似乎是问题的中心:

InstanceA:FindFirstChild("InstanceB")

此行将在 InstanceA 中搜索名为“InstanceB”的实例,如果不存在这样的实例,则这将“返回”一个称为 nil 的值,这是 Luau 表示什么都没有的方式。然后可以像您一样将该值存储在变量中:

local feedbackSound = SoundService:FindFirstChild("FeedbackSound")

此行将在 SoundService 实例中搜索名为“FeedbackSound”的实例。非常重要的是,这个 Sound 实例实际上存在于 SoundService 内部,因为您可以像稍后在脚本中那样对待它。

现在,我无法访问您的位置,所以我只能假设您的问题是:

  • Sound 实例不存在于 SoundService 内部,在这种情况下,您需要在 SoundService 中实际插入一个声音,将其命名为“FeedbackSound”,并设置它的 SoundId 属性,以便它有一个实际的声音可以播放。这将导致 FindFirstChild 返回 nil,从而导致稍后出现问题。
  • Sound 实例
  • 确实存在于 SoundService 内部,但是它的 SoundId 属性未设置rbxassetid://4110925712
    。如果未设置声音的 SoundId,则播放它不会发生任何事情。
这有望解决您的问题:D


以下是您可能需要考虑的一些额外的挑剔:

  • :Play()
    函数不接受声音ID,事实上,括号中没有任何值。所以这条线实际上就很好。 (如果你在括号里输入任何内容,你会得到一个错误)
  • 初学者在写
  • == true
    语句时写“
    if
    ”是一个常见的错误,哎呀,我以前也这样做过!然而,这是完全没有必要的。它所做的就是将 
    CanCollide
     的值与 
    true
     进行比较,如果是 
    true
    ,则结果为 
    true
    ,这是多余的。
© www.soinside.com 2019 - 2024. All rights reserved.