简体   繁体   English

尝试索引一个 nil 值,love2d,lua

[英]Attempt to index a nil value, love2d, lua

I am learning about gui and statemachine with love2d.我正在使用 love2d 学习 gui 和状态机。 I made a simple app which have menu, run and ended state.我做了一个简单的应用程序,它有菜单、运行和结束状态。 However I got an problem in the ended state with the buttons.但是我在按钮的结束状态下遇到了问题。 Here is the update and exit part of ended.lua:下面是 ended.lua 的更新和退出部分:

function m:update(dt, _stateMachine)

    for i = 1, #self.buttons do
        
        self.buttons[i]:detect(dt) ------ Attempt to index a nil value

        if self.buttons[i].waitAnimEnded == true then
                
            if i == 1 then
                    
                _stateMachine:change(gstates.run)

            elseif i == 2 then

                _stateMachine:change(gstates.menu)

            end

        end

    end

function m:exit()

    if gstates.run.score > gstates.run.maxScore then
        
        gstates.run.maxScore = gstates.run.score

    end

    self.buttons = {}
end

However, I got no error with the run.lua, I wrote it with the same way as the ended.lua.但是,我在 run.lua 中没有出错,我用与 ended.lua 相同的方式编写它。 run.lua:运行.lua:

function m:update(dt, _stateMachine)

    for i = 1, #self.buttons do
        
        self.buttons[i]:detect(dt)

        if self.buttons[i].waitAnimEnded == true then

            if i == 1 then
            
                self.score = self.score + 1

                self.buttons[i].waitAnimEnded = false

            elseif i == 2 then

                _stateMachine:change(gstates.ended)

            end

        end

    end
    
end

function m:exit()

    self.buttons = {}
    
end

And here is the stateMachine.lua这是 stateMachine.lua

sm = {}

sm.currentState = gstates.menu

function sm:change(_newState)

    self.lastState = self.currentState

    self.currentState = _newState
        
    self.lastState:exit()

    self.currentState:enter()

end

function sm:update(dt)
    
    self.currentState:update(dt, self)

end

function sm:render()
    
    self.currentState:render()

end

return sm

After I did a little changed in ended.lua, the error fixed, but idk why would that happened.在我对 ended.lua 做了一点改动后,错误就修复了,但我不知道为什么会这样。

function m:update(dt, _stateMachine)

    for i = 1, #self.buttons do

        if not(#self.buttons == 0) then ---- I changed here -----
        
            self.buttons[i]:detect(dt)

            if self.buttons[i].waitAnimEnded == true then
                
                if i == 1 then
                    
                    _stateMachine:change(gstates.run)

                elseif i == 2 then

                    _stateMachine:change(gstates.menu)

                end

            end
        end

    end
    
end

If you have an idea of this problem, please tell me.如果您对这个问题有想法,请告诉我。 Thank you.谢谢你。

Let's assume self.buttons contains two values.假设self.buttons包含两个值。

for i = 1, #self.buttons do
    print(self.buttons[i])
    _stateMachine:change(gstates.ended)
end

This will print a button, and nil.这将打印一个按钮,然后为 nil。 And this nil causes the attempt to index a nil value.这个 nil 导致尝试索引一个 nil 值。 Why?为什么? Because in _stateMachine:change you call self.lastState:exit() , and in exit you remove all buttons via self.buttons = { } and then the second button no longer exists.因为在_stateMachine:change中调用self.lastState:exit() ,而在 exit 中通过self.buttons = { }删除了所有按钮,然后第二个按钮不再存在。

If you change the state, break the button loop.如果更改状态,请中断按钮循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM