【问题标题】:can anyone help me to get the error cleared?谁能帮我清除错误?
【发布时间】:2021-01-13 06:15:27
【问题描述】:

1.i,在games50中实现游戏马里奥时出现错误,错误显示在状态机等中

    function StateMachine:change(stateName, enterParams)
    
        assert(self.states[stateName]) -- state must exist!
        self.current:exit()
        self.current = self.states[stateName]()
        self.current:enter(enterParams)
    end

错误

src/StateMachine.lua:18: attempt to call method 'enter' (a nil value)

追溯

src/StateMachine.lua:18: in function 'change'
main.lua:21: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

【问题讨论】:

  • 你为什么调用self.states[stateName]()?你的程序中的“状态”是什么?

标签: lua love2d


【解决方案1】:

好的。因此,您调用了一个名为“enter”的函数,该函数包含在一个名为“current”的表中,该表包含在另一个表中,该表在函数“change”的隐藏参数“self”中传递。

您需要首先检查“更改”函数是如何调用的。

检查是否是这样的:

xxxx:change(...

而不是:

xxx.change(...

因为在第二个版本中,没有提供隐藏的“self”参数(将包含对“xxx”的引用)。

然后,检查“self.current”是否有效,在self.current = self.states[stateName]() 之后打印:

print(self.current)

如果您在控制台中看到表引用,那很好。

在这个阶段,它会告诉你问题是self.current = self.states[stateName]()返回的表中不存在的“enter”函数。

您的所有代码听起来都像是在使用您不掌握使用的外部库。我建议你用自己的代码开始编写游戏,简单的游戏,不要使用外部代码。

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2020-10-05
    相关资源
    最近更新 更多