修复无法在 Statemachine 中获取从 State:enter() 声明的变量
Fix not able to get variables declared from State:enter() in Statemachine
我正在使用 lua 开发一款游戏,我喜欢基于 flappy bird 的 2D。所以我使用 StateMachine.lua 来管理状态和东西,并且有一些状态 运行 一切正常。我有一个分数状态,我在更改状态时使用参数获取数据并且它有效,但是在未暂停时返回游戏的新状态下,我在 enter 方法中定义的任何内容都未定义。这就是我可以访问数据的 params 变量的方法。我评论了所有内容,甚至在 enter 方法中定义了任意数字以用于测试目的和在 render 方法中渲染,它返回 nil。
如果我在 init 方法中定义,它会起作用,这是我的代码,没有注释代码
PlayStateP = Class{__includes = BaseState}
PIPE_SPEED = 60
PIPE_WIDTH = 70
PIPE_HEIGHT = math.random(270,290)
BIRD_WIDTH = 38
BIRD_HEIGHT = 24
vari = math.random(20,90)
function PlayStateP:init()
self.num =213
end
function PlayStateP:enter(params)
self.bird = params.bird
self.pipePairs = params.pipePairs
self.score = params.score
self.timer = params.timer
self.lastY = params.lastY
self.num= 3
end
function PlayStateP:render()
love.graphics.setFont(flappyFont)
love.graphics.print(tostring(self.pipePairs)..tostring(self.num2), 8, 8)
end
--[[
Called when this state is transitioned to from another state.
]]
function PlayStateP:enter()
-- if we're coming from death, restart scrolling
scrolling = true
end
--[[
Called when this state changes to another state.
]]
function PlayStateP:exit()
-- stop scrolling for the death/score screen
scrolling = false
end
在这种情况下,每当我 运行 这种状态时,文本都是 nilnil。此外,如果我在 init 方法中定义一些变量并在 enter 中更改值,它仍然不会这样做。
我怎样才能克服这个问题并使用通过参数传递的数据并且仍然能够在文件中使用该数据。
如果需要,这里是基础状态文件
BaseState = Class{}
function BaseState:init() end
function BaseState:enter() end
function BaseState:exit() end
function BaseState:update(dt) end
function BaseState:render() end
LUA 没有函数重载的概念。 PlayerStateP:enter()
的第二个定义:
function PlayStateP:enter()
-- if we're coming from death, restart scrolling
scrolling = true
end
覆盖采用 params
的第一个定义。如果您使用参数调用它,它们将被忽略。您需要为函数选择不同的名称。
我正在使用 lua 开发一款游戏,我喜欢基于 flappy bird 的 2D。所以我使用 StateMachine.lua 来管理状态和东西,并且有一些状态 运行 一切正常。我有一个分数状态,我在更改状态时使用参数获取数据并且它有效,但是在未暂停时返回游戏的新状态下,我在 enter 方法中定义的任何内容都未定义。这就是我可以访问数据的 params 变量的方法。我评论了所有内容,甚至在 enter 方法中定义了任意数字以用于测试目的和在 render 方法中渲染,它返回 nil。 如果我在 init 方法中定义,它会起作用,这是我的代码,没有注释代码
PlayStateP = Class{__includes = BaseState}
PIPE_SPEED = 60
PIPE_WIDTH = 70
PIPE_HEIGHT = math.random(270,290)
BIRD_WIDTH = 38
BIRD_HEIGHT = 24
vari = math.random(20,90)
function PlayStateP:init()
self.num =213
end
function PlayStateP:enter(params)
self.bird = params.bird
self.pipePairs = params.pipePairs
self.score = params.score
self.timer = params.timer
self.lastY = params.lastY
self.num= 3
end
function PlayStateP:render()
love.graphics.setFont(flappyFont)
love.graphics.print(tostring(self.pipePairs)..tostring(self.num2), 8, 8)
end
--[[
Called when this state is transitioned to from another state.
]]
function PlayStateP:enter()
-- if we're coming from death, restart scrolling
scrolling = true
end
--[[
Called when this state changes to another state.
]]
function PlayStateP:exit()
-- stop scrolling for the death/score screen
scrolling = false
end
在这种情况下,每当我 运行 这种状态时,文本都是 nilnil。此外,如果我在 init 方法中定义一些变量并在 enter 中更改值,它仍然不会这样做。 我怎样才能克服这个问题并使用通过参数传递的数据并且仍然能够在文件中使用该数据。
如果需要,这里是基础状态文件
BaseState = Class{}
function BaseState:init() end
function BaseState:enter() end
function BaseState:exit() end
function BaseState:update(dt) end
function BaseState:render() end
LUA 没有函数重载的概念。 PlayerStateP:enter()
的第二个定义:
function PlayStateP:enter()
-- if we're coming from death, restart scrolling
scrolling = true
end
覆盖采用 params
的第一个定义。如果您使用参数调用它,它们将被忽略。您需要为函数选择不同的名称。