【问题标题】:How can I change color depending on health?如何根据健康状况改变颜色?
【发布时间】:2023-02-10 17:19:26
【问题描述】:

我想知道如何编辑下面的脚本,使颜色根据健康量而不是脚本中的设定量来降低或升高。目前一切正常,我只是想编辑脚本来完成我上面所说的。

local parts = script.Parent.Parent:WaitForChild("Collectables"):GetChildren()

for i, v in pairs(parts) do
    if v:IsA("Part") then
        if v:WaitForChild("ClickDetector") then
            if v:FindFirstChild("Values") then
                local Values = v:FindFirstChild("Values")
                Values.Red.Value = 0
                Values.Green.Value = 255
                Values.Blue.Value = 0
                Values.Health.Value = 100
                Values.Change.Value = true
                v.SurfaceGui.TextLabel.Text = 100
                Values.Color.Value = Color3.fromRGB(Values.Red.Value,Values.Green.Value,Values.Blue.Value)
                v.SurfaceGui.TextLabel.TextColor3 = Values.Color.Value
                v.ClickDetector.MouseClick:Connect(function()
                    if Values.Health.Value > 5 then
                        Values.Health.Value -= 5
                    else
                        v:Destroy()
                    end
                    if v:FindFirstChild("SurfaceGui") then
                        v.SurfaceGui.TextLabel.Text = v.SurfaceGui.TextLabel.Text - 5
                        if Values.Change.Value == true then
                            if Values.Red.Value < 250 then
                                Values.Red.Value += 50
                            elseif Values.Red.Value == 250 then
                                Values.Red.Value += 5
                            elseif Values.Red.Value == 255 and Values.Green.Value > 5 then
                                Values.Green.Value -= 50
                            elseif Values.Green.Value == 5 then
                                Values.Green.Value -= 5
                                Values.Change.Value = false
                            end
                            Values.Color.Value = Color3.fromRGB(Values.Red.Value,Values.Green.Value,Values.Blue.Value)
                            v.SurfaceGui.TextLabel.TextColor3 = Values.Color.Value
                        end
                    end
                end)
            end
        end
    end
end

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    与其使用逐步函数来计算颜色,更简单的处理方法可能是提前定义您想要的颜色或将其简化为基于健康的颜色范围。喜欢,

    • 如果它在 100 - 60 之间,则颜色为绿色。
    • 如果它在 60 - 30 之间,它就是黄色的。
    • 如果小于 30,则为红色。

    为了让颜色对健康做出反应,我会在 NumberValue 对象上使用 Changed signal 设置一系列依赖关系。

    ClickDetector 的唯一工作是修改块的健康状况。但是当健康发生变化时,颜色值也会发生变化。当颜色值改变时,文本颜色也会改变。

    首先,删除 R、G 和 B NumberValues,您可以在没有它们的情况下设置文本颜色。

    local DEFAULT_HEALTH = 100
    local HEALTH_PER_CLICK = 5
    -- colors are sorted by health in descending order, add more as desired
    local COLORS = {
        [60] = Color3.fromRGB(  0, 255, 0), -- green
        [30] = Color3.fromRGB(255, 255, 0), -- yellow
        [0]  = Color3.fromRGB(255,   0, 0), -- red
    }
    
    local function observeHealthChanges(values, textLabel)
        -- whenever the health changes, update the text and the text color
        local health = values.Health
        health.Changed:Connect(function(newValue)
            textLabel.Text = tostring(newValue)
    
            -- set the color based on the health by looping through the colors to find the right ones
            for colorRange, healthColor in pairs(COLORS) do
                if newValue > colorRange then
                    textLabel.TextColor3 = healthColor
                    break
                end
            end
        end)
    end
    
    local function observeClicks(part, values, detector)
        local health = values.Health
    
        -- listen for clicks to reduce the health            
        detector.MouseClick:Connect(function()
            if health.Value > HEALTH_PER_CLICK then
                health.Value -= HEALTH_PER_CLICK
            else
                part:Destroy()
            end
        end)
    end
    
    local parts = script.Parent.Parent:WaitForChild("Collectables"):GetChildren()
    for _, v in ipairs(parts) do
        -- escape if the object isn't a part
        if v:IsA("Part") then
            
            -- escape if the part doesn't have Values, a ClickDetector, and a SurfaceGui
            local Values = v.Values
            local Detector = v.ClickDetector
            local Gui = v.SurfaceGui
            if Values and Detector and Gui then
            
                -- register the change listeners
                local textLabel = v.SurfaceGui.TextLabel
                observeHealthChanges(Values, textLabel)
                observeClicks(v, Values, Detector)
    
                -- set the health and all the rest will follow
                Values.Health.Value = DEFAULT_HEALTH
            end
        end
    end
    

    【讨论】:

    • 我只是把它放在我的游戏中,这是行不通的。它是否意味着在本地脚本中?它位于常规脚本中的单独文件夹中。
    • 应该像您的原始脚本一样工作。什么不起作用?更改后的信号是否触发?颜色不正确?
    • 所有事件都在触发,但颜色一直都是红色,没有变化。红色不是设置的 TextColor3 ,因此脚本可以正常工作,但它唯一的设置是红色
    • 很抱歉这么晚才回复,我已经有一段时间无法处理我的游戏了,但是在输入打印语句后,我注意到 observeHealthChanges 和 observeClicks 函数触发了,但 observeColorChanges 函数没有触发。
    • @TrulyBlue,我更新了我的答案并简化了它。这应该可以防止信号相互冲突。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多