程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用变量将 draw.RoundedBox 添加到 x 位置?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用变量将 draw.RoundedBox 添加到 x 位置??

开发过程中遇到如何使用变量将 draw.RoundedBox 添加到 x 位置?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用变量将 draw.RoundedBox 添加到 x 位置?的解决方法建议,希望对你解决如何使用变量将 draw.RoundedBox 添加到 x 位置?有所启发或帮助;

我目前正在尝试创建一个 XP 系统,让您可以通过杀死 NPC 来获得经验和升级。我能够设置实际的 XP 栏并将宽度设置为我的变量“xp”。当我将“xp”设置为 100 之类的值时,它会显示一个宽度为 100 的条形。但是,当我添加一个函数钩子时,该钩子会在玩家杀死 NPC 时激活以将 +1 添加到变量“xp”中,它确实如此不影响条的宽度。是否有另一种方法我应该解决这个问题,以便 XP bar 使用变量“xp”进行更新。

--init.lua file--

Addcsluafile("cl_init.lua")

levels = 0
xp = 0

hook.Add("OnNPCKilled","npcreWARD",function()

    xp = xp + 1
    print(xp)

end)

--cl_init.lua file--

hook.Add("HUDPaint","DrawMyHud",function()
    --XP bar BACkground
    draw.RoundedBox(0,1598,8,300+4,30 + 4,color(12,120,150))
    --XP bar Progress bar
    draw.RoundedBox(0,1600,10,xp,30,color(28,174,214))
end)

解决方法

这里有几件事情你有错误,以及一些解决你问题的方法。

作为基础层: 您发布了 2 个文件示例。 init.lua(哪个是服务器) cl_init.lua(只有客户端可以看到)

这里的第一个问题是你在服务器端增加一个全局变量(XP),它没有发送到客户端。 这个变量必须分配给玩家,并联网(从服务器发送到客户端),否则客户端将永远无法看到发生了什么。

你可以做的是像这样分配变量: https://wiki.facepunch.com/gmod/GM:OnNPCKilled

-- init.lua --

util.AddNetworkString("npcreWARD:givexp")
hook.Add("OnNPCKilled","NPCReWARD",function(npc,killer,weapon) -- NPC is the npc you are wanTing to kill,killer is you,weapon is what was used to kill the NPC

net.Start("npcreWARD:givexp")
net.WriteInt(1,8)
net.Send(killer)
 -- what this will do is send the amount of xp you want to give to the client (in this case,you),to the client,so they can print it somewhere,in this case,a hud / progress bar.

end)

-- cl_init.lua -- 在这里,您将像以前一样绘制框/圆框,但您错过的一件事是定义最大 xp 量是多少。@R_250_7724@,您将无法制作动态 XP 栏,该栏会根据您拥有的 XP 数量而变化。

您应该研究的其他一些事情是按屏幕宽度/屏幕高度(ScrW()、ScrH())缩放,因为您现在制作的内容可能在您的屏幕上看起来不错,但可能不是我的,如果我运行不同的分辨率。如果您希望这对其他人有用,请务必虑这一点。

为了这篇文章的目的,我将放一个例子来说明这个进度条是如何工作的,以及来自 init.lua 的代码。

--[[
    Everything that happens here,in the client realm,is only shared with the client.
    Unless you network something to the server,it will be unknown to the server,until you have told it whatever you want to send.
]]

local xp = 0
local maxExperience = 100
--[[
    If you look at the example in init.lua,you can see that this is the opposite of sending. We are now receiving the data that the server has sent to us.
]]
net.Receive("npcreWARD:givexp",function()
    local xpReWARD = net.ReadInt(8)

    --[[
        PrinTing xpReWARD should now give us (1),for the first NPC we kill.
        It will keep prinTing 1,because that's the amount of XP we receive.
    
        Using the variable "xp",that we have defined above,allows us to keep track of it,for this session,or until the code is refrehsed.
    
    The below code does this:
    I want xp to be the value of xp + the xp reWARD that I got from the server
    you can do the opposite if you want to remove XP,just make the "+" a "-"
    ]]
    xp = xp + xpReWARD

    --[[
        what we have now acheived is that the client now knows how much XP we have.
        We can now move on to painTing this on our screen
    ]]
end)

hook.Add("HUDPaint","npcreWARD:prog",function()

    -- Draw a BACkground box

    draw.RoundedBox(8,ScrW()*.5,ScrH()*.7,ScrW()*.3,ScrH()*.1,Color(33,33,255))

   
    --[[
        Draw the same box,but this one will show how much progress you have made,so it will receive a lighter shade of gray.
         To explain in short what we are doing here.
        We are placing the box on the middle of our screen
        The box will be placed at 70% of screen height (near bottom)
        It will be 30% of the screen width wide
        And 10% of the screen height tall

        In basic terms what we are going to do,we are going to make the width change based on how much XP you have,and how much XP you need to achieve for the bar to be full
        
        So basic math:
        (maxExperience is 100xp)
            xp / maxExperience = 0 (Currently)

            if we receive 1 xp
            xp / maxExperience = 0.01
            what this means is that if we have 1 xp,1% of the bar will draw.
            If you get 50 xp 50% of the bar will draw.
        ]]
    draw.RoundedBox(8,ScrW()*.3 * math.Clamp((xp / maxExperiencE),1),Color(66,66,255))

end)

从这点开始,我建议学习net库的使用,了解服务端和客户端的区别,不要犹豫,问任何问题。

您也可以查看 SetNWInt、GetNWInt,但我制作这个示例是为了向您展示联网方式。 https://wiki.facepunch.com/gmod/Entity:SetNWInt https://wiki.facepunch.com/gmod/Entity:GetNWInt

还必须让您知道这里给出的所有代码都未经测试,并且可能包含错误,但我希望它能让您基本了解为什么您的代码不起作用,以及您必须做什么它起作用了。

祝你在未来的编码工作中好运!

大佬总结

以上是大佬教程为你收集整理的如何使用变量将 draw.RoundedBox 添加到 x 位置?全部内容,希望文章能够帮你解决如何使用变量将 draw.RoundedBox 添加到 x 位置?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。