Lua递归滚动菜单不起作用?

发布于 2024-11-10 12:12:06 字数 4529 浏览 2 评论 0原文

嘿伙计们,只是想知道为什么我的菜单不起作用,我已经编码了大约 8 个小时,但就是不知道出了什么问题。

Menu = {
    label = "Mahin Menu",
    current = current or true,
    open = open or true,
    subMenus = {}
}

function Menu.newSubMenu()
    return {
        setup = Menu.setup,
        print = Menu.print,
        toggleOpen = Menu.toggleOpen,
        getCurrentMenu = Menu.getCurrentMenu,
        getLastMenu = Menu.getLastMenu,
        getNextMenu = Menu.getNextMenu,
        getPrevMenu = Menu.getPrevMenu
    }
end

function Menu:setup(m_parent, m_label, m_action)
    self.parent = m_parent
    self.label = m_label
    self.action = m_action
    self.subMenus = {}
    self.current = false
    self.open = false
    table.insert(m_parent.subMenus, self)
end

function Menu:print(indent)
    io.write(string.rep("    ", indent))
    if #self.subMenus>0 then
        if self.open == true then
            io.write("[-]")
        else
            io.write("[+]")
        end
    else
        io.write("   ")
    end
    if self.current == true then
        io.write("<" .. self.label .. ">")
    else
        io.write(" " .. self.label)
    end
    io.write("\n")
    if #self.subMenus>0 and self.open == true then
        for i=1,#self.subMenus do
            self.subMenus[i]:print(indent+1)
        end
    end
end

function Menu:toggleOpen()
    if self.open == true then
        self.open = false
    else
        self.open = true
    end
end

function Menu:getCurrentMenu()
    if self.current == true then
        return self
    else
        for k=1,#self.subMenus do
            local v = self.subMenus[k]:getCurrentMenu()
            if v ~= nil then
                return v
            end
        end
    end
end

function Menu:getLastMenu()
    if self.open == true and #self.subMenus > 0 then
        return self.subMenus[#self.subMenus]:getLastMenu()
    else
        return self
    end
end

function Menu:getNextMenu(bool)
    bool = bool or false
    if bool == false then
        if #self.subMenus > 0 and self.open == true then
            return self.subMenus[1]
        end
    end
    if self.parent then
        if self.parent.subMenus[#self.parent.subMenus] == self then
            self.parent:getNextMenu(true)
        else
            for i=1,#self.parent.subMenus do
                if self.parent.subMenus[i] == self then
                    print(self.parent.subMenus[i+1].label)
                    return self.parent.subMenus[i+1]
                end
            end
        end
    else
        return self
    end
end

function Menu:getPrevMenu()
    if self.parent then
        for k=1,#self.parent.subMenus do
            if self.parent.subMenus[k] == self then
                if k == 1 then
                    return self.parent
                elseif #self.parent.subMenus[k-1].subMenus > 0 and self.parent.subMenus[k-1].open == true then
                    local x = self.parent.subMenus[k-1]
                    while #x.subMenus > 0 and x.open == true do
                        x = x.subMenus[#x.subMenus]
                    end
                    return x
                else
                    return self.parent.subMenus[k-1]
                end
            end
        end
    else
        return self
    end
end

Test = Menu.newSubMenu()
Test:setup(Menu, "Test item")
Mahi = Menu.newSubMenu()
Mahi:setup(Menu, "Mahi item")
Mahi.open = true
Testx = Menu.newSubMenu()
Testx:setup(Mahi, "Lalall")
Testx.open= true
Sadmad = Menu.newSubMenu()
Sadmad:setup(Testx, "Woot")
Asd = Menu.newSubMenu()
Asd:setup(Menu, "Asd menu")
Asd.current = true
Menu.current = false

repeat
    print(string.rep("\n",2))
    Menu:print(0)
    x=io.read()
    if x == "z" then
        x = Menu:getCurrentMenu()
        print(Menu:getCurrentMenu().label)
        print(Menu:getCurrentMenu():getNextMenu().label)
        y = Menu:getCurrentMenu():getNextMenu()
        x.current = false
        y.current = true
    elseif x == "a" then
        x = Menu:getCurrentMenu()
        y = Menu:getCurrentMenu():getPrevMenu()
        x.current = false
        y.current = true
    end
until x == "sad"

” 有代码,当我尝试将当前的“Asd 菜单”向下移动时,它会出错:

menu.lua:150: attempt to index a nil value

这没有任何意义,它已明确声明,并且我尝试添加打印,它们总是给我 Asd菜单哦 如果我尝试从 Woot 移动到 Asd 菜单,同样会出现同样的错误,我不知道为什么,因为我添加了这些打印

print(Menu:getCurrentMenu().label)
print(Menu:getCurrentMenu():getNextMenu().label)

,它们确实给了我 Asd 菜单,但随后它说尝试索引一个 nil值在第二个打印行,但它仍然打印?我没有想法,这里有什么帮助吗?

Hey guys, just wondering why ain't my menu working, I've been coding it for like 8 hours now and just can't figure out what's wrong.

Menu = {
    label = "Mahin Menu",
    current = current or true,
    open = open or true,
    subMenus = {}
}

function Menu.newSubMenu()
    return {
        setup = Menu.setup,
        print = Menu.print,
        toggleOpen = Menu.toggleOpen,
        getCurrentMenu = Menu.getCurrentMenu,
        getLastMenu = Menu.getLastMenu,
        getNextMenu = Menu.getNextMenu,
        getPrevMenu = Menu.getPrevMenu
    }
end

function Menu:setup(m_parent, m_label, m_action)
    self.parent = m_parent
    self.label = m_label
    self.action = m_action
    self.subMenus = {}
    self.current = false
    self.open = false
    table.insert(m_parent.subMenus, self)
end

function Menu:print(indent)
    io.write(string.rep("    ", indent))
    if #self.subMenus>0 then
        if self.open == true then
            io.write("[-]")
        else
            io.write("[+]")
        end
    else
        io.write("   ")
    end
    if self.current == true then
        io.write("<" .. self.label .. ">")
    else
        io.write(" " .. self.label)
    end
    io.write("\n")
    if #self.subMenus>0 and self.open == true then
        for i=1,#self.subMenus do
            self.subMenus[i]:print(indent+1)
        end
    end
end

function Menu:toggleOpen()
    if self.open == true then
        self.open = false
    else
        self.open = true
    end
end

function Menu:getCurrentMenu()
    if self.current == true then
        return self
    else
        for k=1,#self.subMenus do
            local v = self.subMenus[k]:getCurrentMenu()
            if v ~= nil then
                return v
            end
        end
    end
end

function Menu:getLastMenu()
    if self.open == true and #self.subMenus > 0 then
        return self.subMenus[#self.subMenus]:getLastMenu()
    else
        return self
    end
end

function Menu:getNextMenu(bool)
    bool = bool or false
    if bool == false then
        if #self.subMenus > 0 and self.open == true then
            return self.subMenus[1]
        end
    end
    if self.parent then
        if self.parent.subMenus[#self.parent.subMenus] == self then
            self.parent:getNextMenu(true)
        else
            for i=1,#self.parent.subMenus do
                if self.parent.subMenus[i] == self then
                    print(self.parent.subMenus[i+1].label)
                    return self.parent.subMenus[i+1]
                end
            end
        end
    else
        return self
    end
end

function Menu:getPrevMenu()
    if self.parent then
        for k=1,#self.parent.subMenus do
            if self.parent.subMenus[k] == self then
                if k == 1 then
                    return self.parent
                elseif #self.parent.subMenus[k-1].subMenus > 0 and self.parent.subMenus[k-1].open == true then
                    local x = self.parent.subMenus[k-1]
                    while #x.subMenus > 0 and x.open == true do
                        x = x.subMenus[#x.subMenus]
                    end
                    return x
                else
                    return self.parent.subMenus[k-1]
                end
            end
        end
    else
        return self
    end
end

Test = Menu.newSubMenu()
Test:setup(Menu, "Test item")
Mahi = Menu.newSubMenu()
Mahi:setup(Menu, "Mahi item")
Mahi.open = true
Testx = Menu.newSubMenu()
Testx:setup(Mahi, "Lalall")
Testx.open= true
Sadmad = Menu.newSubMenu()
Sadmad:setup(Testx, "Woot")
Asd = Menu.newSubMenu()
Asd:setup(Menu, "Asd menu")
Asd.current = true
Menu.current = false

repeat
    print(string.rep("\n",2))
    Menu:print(0)
    x=io.read()
    if x == "z" then
        x = Menu:getCurrentMenu()
        print(Menu:getCurrentMenu().label)
        print(Menu:getCurrentMenu():getNextMenu().label)
        y = Menu:getCurrentMenu():getNextMenu()
        x.current = false
        y.current = true
    elseif x == "a" then
        x = Menu:getCurrentMenu()
        y = Menu:getCurrentMenu():getPrevMenu()
        x.current = false
        y.current = true
    end
until x == "sad"

"
there's the code, and when ever i try to move my current from "Asd menu" downwards, it'll error:

menu.lua:150: attempt to index a nil value

which doesn't make any sense, it's clearly declared, and I've tried adding prints and they always give me Asd menu O.o
Same goes for if I'll try to move from Woot to Asd menu, same exact error, and I have no idea why, since I added those prints

print(Menu:getCurrentMenu().label)
print(Menu:getCurrentMenu():getNextMenu().label)

and they do give me Asd menu, but then it says that trying to index a nil value at the second print line, but it sill does print? I'm out of ideas, any help out here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2024-11-17 12:12:06

第 92 行缺少 return 语句。

请注意,该行实际上不返回任何内容,因此该函数返回 nil。
将其更改为 return self.parent:getNextMenu(true) 后,它似乎可以正常工作。

You are missing a return statement in line 92.

Note that this line does not actually return anything, so the function is returning nil.
After changing it to return self.parent:getNextMenu(true) it seems to be working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文