【问题标题】:Love2d. States system won't load the game state a second time爱2d。状态系统不会第二次加载游戏状态
【发布时间】:2016-04-30 06:08:21
【问题描述】:

我只是在学习 lua 和 love2d,我正在尝试制作一个示例游戏来熟悉。我在 youtube 上关注 goature 的教程,并使用他的状态系统来导航游戏。我完成了菜单示例代码的编写,并决定我希望能够从游戏中返回菜单。问题是游戏不会第二次加载游戏状态,而是给我一个灰屏。这应该是相关代码。

来自根文件 main.lua

function clearLoveCallbacks()
    love.draw = nil
    love.joystickpressed = nil
    love.joystickreleased = nil
    love.keypressed = nil
    love.keyrelease = nil
    love.load = nil
    love.mousepressed = nil
    love.mousereleased = nil
    love.update = nil
end

states = {}
function loadState(name)
    states = {}
    clearLoveCallbacks()
    local path = "states/" .. name
    require(path .. "/main")
    load()
end

function load()
end

function love.load()
    loadState("menu")

end

从菜单 main.lua

function load()

    love.graphics.setBackgroundColor(190, 190, 190, 255)

    imgPlay = love.graphics.newImage("Textures/start.png")
    imgPlayOn = love.graphics.newImage("Textures/start_on.png")
    imgexit = love.graphics.newImage("Textures/exit.png")
    imgexitOn = love.graphics.newImage("Textures/exit_on.png")

    buttons = {
              {imgOff = imgPlay, imgOn = imgPlayOn, x = 400, y = 300 - 64, w = 256, h = 64, action = "play"},
              {imgOff = imgexit, imgOn = imgexitOn, x = 400, y = 300 + 64, w = 256, h = 64, action ="exit"}
              }
end
function love.mousepressed(x, y, button)
    if button == 1 then
        for k,v in pairs(buttons) do
            local ins = insideBox(x, y, v.x - (v.w/2), v.y - (v.h/2), v.w, v.h) -- must use v. for the rest of the arguments except the first 2 because that x and y defined in arguments of the function

            if ins then
                if v.action == "play" then
                    loadState("New_Game") -- loads new game for some reason if i load the menu state from game it won't load the game a secound time
                elseif v.action == "exit" then
                    love.event.quit() -- love2d function to quit
                end
            end
        end
    end
end

并且来自 New_Game main.lua

function load()
    require("States/New_Game/entities")
    love.graphics.setBackgroundColor(255,255,255,255)
    ents.startup()
    --local newEnt= ents.Create("new_ent", 128, 128)
    ents.Create("enemy_base", -math.random(128, 256), 128)
end
function love.keypressed(key, unicode)
    if love.keyboard.isDown("escape") then
        loadState("Menu")
    end
end

【问题讨论】:

    标签: lua love2d


    【解决方案1】:

    您提供的代码,如您所愿,无法实现您想要的。

    您似乎希望能够调用loadState,并且希望此函数将内容加载到有效设置新“状态”的全局变量中。

    嗯,这可能没问题...除了您使用require 进行加载。而且,除非 Love2D 改变了函数的工作方式,否则require 只会加载特定的脚本一次

    看,改变状态的是脚本本身的执行。它定义了一个全局 load 函数,你可以调用它。它在love 表中定义了一些其他函数。以此类推。

    但这只会在脚本加载并执行时发生。如前所述,require 只会在第一次您需要新模块时这样做。

    大概你想要的是dofile,而不是require。但老实说,我建议重新构建您的系统以不依赖全局变量。预先注册您想要的状态,但不要让它们设置全局值。相反,让每个州定义一个包含其load 函数的表。 load 函数应该注册它的 Love2D 回调。

    你的“状态”看起来更像这样:

    local ret = {}
    
    --Executed only once.
    local imgPlay = love.graphics.newImage("Textures/start.png")
    local imgPlayOn = love.graphics.newImage("Textures/start_on.png")
    local imgexit = love.graphics.newImage("Textures/exit.png")
    local imgexitOn = love.graphics.newImage("Textures/exit_on.png")
    
    local buttons =
    {
        {imgOff = imgPlay, imgOn = imgPlayOn, x = 400, y = 300 - 64, w = 256, h = 64, action = "play"},
        {imgOff = imgexit, imgOn = imgexitOn, x = 400, y = 300 + 64, w = 256, h = 64, action ="exit"}
    }
    
    local function mousepressed(x, y, button)
        if button == 1 then
            for k,v in pairs(buttons) do
                local ins = insideBox(x, y, v.x - (v.w/2), v.y - (v.h/2), v.w, v.h) -- must use v. for the rest of the arguments except the first 2 because that x and y defined in arguments of the function
    
                if ins then
                    if v.action == "play" then
                        loadState("New_Game") -- loads new game for some reason if i load the menu state from game it won't load the game a secound time
                    elseif v.action == "exit" then
                        love.event.quit() -- love2d function to quit
                    end
                end
            end
        end
    end
    
    function ret.load()
        --Actual state setup.
        love.mousepressed = mousepressed
    
        love.graphics.setBackgroundColor(190, 190, 190, 255)
    end
    
    return ret
    

    您的loadState 函数也需要更改:

    function loadState(name)
        states = {}
        clearLoveCallbacks()
        local path = "states/" .. name
        local state = require(path .. "/main")
        state.load()
    end
    

    哦,我建议你放弃你正在“学习”的任何教程。它在教你可怕的事情。它涉及方式过多的全局使用,这就是导致此问题的原因。如果你只是在学习 Lua,那么你在学习使用 Lua 时会非常糟糕。

    【讨论】:

    • 感谢这有帮助,但是像您所说的那样的桌子会是什么样子?你知道有什么好的 lua 教程吗?
    • @Jacques:我添加了一个示例。
    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 2019-07-03
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多