修复了无法从Statemachine中的State:enter声明的变量的问题

问题描述

我正在开发使用lua的游戏,并且喜欢基于飞鸟的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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)