错误:src/statemachine.lua:18:尝试调用方法' enter' (零值)
我正在使用它来在我的LUA项目中建立一些基本的OO概念: https://github.com/vrld/hump/hump/hump/blob/blob/master/master/class/class/class/class。 lua
这是我试图继承
BaseState = Class{}
function BaseState:init() end
function BaseState:enter() end
function BaseState:exit() end
function BaseState:update(dt) end
function BaseState:render() end
在这里的类:
TitleState = Class{__includes = BaseState}
local highlighted = 1
function TitleState:update()
-- toggle highlighted option if we press an arrow key up or down
if love.keyboard.wasPressed('up') or love.keyboard.wasPressed('down') then
highlighted = highlighted == 1 and 2 or 1
-- gSounds['paddle-hit']:play()
end
-- confirm whichever option we have selected to change screens
if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
-- gSounds['confirm']:play()
if highlighted == 1 then
gStateMachine:change('play')
else
love.event.quit()
end
end
end
function TitleState:render()
local backgroundWidth = gTextures['background']:getWidth()
local backgroundHeight = gTextures['background']:getHeight()
love.graphics.draw(gTextures['background'], 0, 0, 0, VIRTUAL_WIDTH / (backgroundWidth - 1), VIRTUAL_HEIGHT / (backgroundHeight - 1))
love.graphics.setFont(gFonts['large'])
love.graphics.printf("Welcome to adventure", 0, VIRTUAL_HEIGHT / 3, VIRTUAL_WIDTH, 'center')
-- instructions
love.graphics.setFont(gFonts['medium'])
-- if we're highlighting 1, render that option blue
if highlighted == 1 then
love.graphics.setColor(103/255, 255/255, 255/255, 255/255)
end
love.graphics.printf("START", 0, VIRTUAL_HEIGHT / 2 + 70,
VIRTUAL_WIDTH, 'center')
-- reset the color
love.graphics.setColor(255/255, 255/255, 255/255, 255/255)
-- render option 2 blue if we're highlighting that one
if highlighted == 2 then
love.graphics.setColor(103/255, 255/255, 255/255, 255/255)
end
love.graphics.printf("QUIT", 0, VIRTUAL_HEIGHT / 2 + 90,
VIRTUAL_WIDTH, 'center')
-- reset the color
love.graphics.setColor(255/255, 255/255, 255/255, 255/255)
end
当我的Statemachine试图调用Enter函数时,正在遇到上述错误。此错误的简单解决方案是,我在滴定器中定义了一个空的Enter函数,但这并不能解决问题,而忽略了更大的问题,即由于某种原因,我没有继承父方法,而我不确定为什么。
如果需要上下文,则while项目在这里: https://github.com/kwchristopher/kwchristopher/kwchristopher/untitled_rpg
我正在按照CS50G的示例来创建我自己的2D RPG,并且可以在此处找到一个更好的工作示例: https:// https:// https:// github.com/games50/zelda
I am using this to establish some basic OO concepts in my lua project:
https://github.com/vrld/hump/blob/master/class.lua
This is the class that I am trying to inherit
BaseState = Class{}
function BaseState:init() end
function BaseState:enter() end
function BaseState:exit() end
function BaseState:update(dt) end
function BaseState:render() end
into here:
TitleState = Class{__includes = BaseState}
local highlighted = 1
function TitleState:update()
-- toggle highlighted option if we press an arrow key up or down
if love.keyboard.wasPressed('up') or love.keyboard.wasPressed('down') then
highlighted = highlighted == 1 and 2 or 1
-- gSounds['paddle-hit']:play()
end
-- confirm whichever option we have selected to change screens
if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
-- gSounds['confirm']:play()
if highlighted == 1 then
gStateMachine:change('play')
else
love.event.quit()
end
end
end
function TitleState:render()
local backgroundWidth = gTextures['background']:getWidth()
local backgroundHeight = gTextures['background']:getHeight()
love.graphics.draw(gTextures['background'], 0, 0, 0, VIRTUAL_WIDTH / (backgroundWidth - 1), VIRTUAL_HEIGHT / (backgroundHeight - 1))
love.graphics.setFont(gFonts['large'])
love.graphics.printf("Welcome to adventure", 0, VIRTUAL_HEIGHT / 3, VIRTUAL_WIDTH, 'center')
-- instructions
love.graphics.setFont(gFonts['medium'])
-- if we're highlighting 1, render that option blue
if highlighted == 1 then
love.graphics.setColor(103/255, 255/255, 255/255, 255/255)
end
love.graphics.printf("START", 0, VIRTUAL_HEIGHT / 2 + 70,
VIRTUAL_WIDTH, 'center')
-- reset the color
love.graphics.setColor(255/255, 255/255, 255/255, 255/255)
-- render option 2 blue if we're highlighting that one
if highlighted == 2 then
love.graphics.setColor(103/255, 255/255, 255/255, 255/255)
end
love.graphics.printf("QUIT", 0, VIRTUAL_HEIGHT / 2 + 90,
VIRTUAL_WIDTH, 'center')
-- reset the color
love.graphics.setColor(255/255, 255/255, 255/255, 255/255)
end
and am getting the above error when my StateMachine attempts to call the enter function. The simple fix for this error is that I define an empty enter function in the TitleState but that doesn't fix the issue and ignores the bigger issue that for some reason I'm not inheriting the parent methods and I'm not sure why.
The while project is here if you need context: https://github.com/kwchristopher/Untitled_Rpg
I am following the examples from CS50g to create my own 2d RPG and a better working example can be found here: https://github.com/games50/zelda
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下行
titlestate = class {__ inception = basestate}
假设
basestate
已经定义,但是在
依赖项中。lua
basestate
类是在titlestate
移动
之后定义的。需要'src/states/stasestate'
两排The following line
TitleState = Class{__includes = BaseState}
assumes that
BaseState
is already definedBut in
Dependencies.lua
theBaseState
class is defined afterTitleState
Move
require 'src/states/BaseState'
two lines up