Lua 为什么在参数中赋值时该值变为零?
我在课程中做了一些工作,使用Love2D更新了他们关于塞尔达的传说,这意味着我必须使用Lua在游戏中实现pots。我建立的所有代码都可以正常工作,但当我按下enter键携带罐子时,我出现了一个错误:Lua 为什么在参数中赋值时该值变为零?,lua,null,love2d,Lua,Null,Love2d,我在课程中做了一些工作,使用Love2D更新了他们关于塞尔达的传说,这意味着我必须使用Lua在游戏中实现pots。我建立的所有代码都可以正常工作,但当我按下enter键携带罐子时,我出现了一个错误: Error src/states/entity/player/PlayerIdlePotState.lua:15: attempt to index local 'params' (a nil value) Traceback src/states/entity/player/PlayerI
Error
src/states/entity/player/PlayerIdlePotState.lua:15: attempt to index local 'params' (a nil value)
Traceback
src/states/entity/player/PlayerIdlePotState.lua:15: in function 'enter'
src/StateMachine.lua:19: in function 'change'
src/Entity.lua:73: in function 'changeState'
src/world/Room.lua:297: in function 'update'
src/world/Dungeon.lua:123: in function 'update'
src/states/game/PlayState.lua:50: in function 'update'
src/StateMachine.lua:23: in function 'update'
main.lua:51: in function 'update'
[C]: in function 'xpcall'
我检查了我的代码,在这个过程中它看起来不错,然后我检查了Room.lua
,我看到了:
if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
if object.type == 'pot' then
if self.player.HitBoxPot:collides(object) then
self.player:changeState('idle-pot', {pot = object})
table.remove(self.objects, k)
end
end
end
在我的update函数中,我在其enter函数中向PlayerIdlePotState.lua发出一个参数:
function PlayerIdlePotState:enter(params)
-- render offset for spaced character sprite
self.entity.offsetY = 5
self.entity.offsetX = 0
self.pot = params.pot
self.entity:changeAnimation('idle-pot-' .. self.entity.direction)
end
不确定是什么导致了nil值。你知道为什么吗
如果您对代码的外观感兴趣,如果您需要,我会将其发布在GitHub上
编辑:我在GitHub上的代码在这里:。如果您看不到任何代码或任何东西,它可能是私有的。让我知道。在
PlayerIdlePotState:enter(参数)
中输入与在love.keyboard.was中输入不同('return')
。我猜您在其他地方调用了PlayerIdlePotState:enter(params)
,但可能在不传递任何参数的情况下调用它?像:enter()
?尝试搜索:在所有代码中输入(
。如果你找不到任何东西,你可以将整个游戏上传到github,我会查看一下。输入PlayerIdlePotState:enter(params)
与输入love.keyboard.was('return')不同
。我猜你在其他地方调用了PlayerIdlePotState:enter(params)
,但可能在不传递任何参数的情况下调用它?比如:enter()
?尝试在所有代码中搜索:enter(
)。如果你找不到任何东西,你可以将整个游戏上传到github,我会看一看。