错误:src/statemachine.lua:18:尝试调用方法' enter' (零值)

发布于 2025-02-09 04:42:31 字数 2923 浏览 2 评论 0原文

我正在使用它来在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2025-02-16 04:42:31

以下行
titlestate = class {__ inception = basestate}
假设basestate已经定义,

但是在依赖项中。lua basestate类是在titlestate

移动之后定义的。需要'src/states/stasestate'两排

The following line
TitleState = Class{__includes = BaseState}
assumes that BaseState is already defined

But in Dependencies.lua the BaseState class is defined after TitleState

Move require 'src/states/BaseState' two lines up

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文