3

My friend and I have been working on a game in love2d recently, but in the early stages of developing my computer hard drive stopped working meaning only my friend could work on it. Now I have a computer and I want to make a main menu in Love2d but There is alot of code in the love.load function (generation of the world and such). My question is can I change what is in love.load when the game is running? e.g The main menu loads up, then the generation of the world loads up when the start button is pressed.

4

1 回答 1

3

love.load函数只运行一次。正如您所提到的,它通常用于设置数据结构和预加载其他资源。您可以使用它来处理世界预加载和菜单预加载。然后,通过某种状态控制什么是活动的。一个简化的例子:

local state = "menu"

function love.load()
    preLoadMenu()
    preLoadWorld()
end

function love.update(dt)
    if state == "menu" then
        updateMenu()
    else
        updateWorld()
    end
end

function love.draw()
    if state == "menu" then
        drawMenu()
    else
        drawWorld()
    end
end

function love.mousepressed(x, y, button)   
    if startClicked(x,y,button) then
        state = "world"
    end
end

可以想象,您绝对不想在加载时为您的游戏预加载所有内容。也许你的游戏太大了。如果是这种情况,请考虑使用活动场景。场景可能是菜单,也可能是游戏关卡。同样,一个简化的例子:

local currentScene

function love.load()
    currentScene = loadMenu()
end

function love.update(dt)
    currentScene.update(dt)       
end

function love.draw()
    currentScene.draw()       
end

function love.mousepressed(x, y, button)   
    if startClicked(x,y,button) then            
        currentScene = loadWorld()
    end
end

从长远来看,第二种选择更加灵活。它可以处理任意数量和类型的场景,而无需为每个场景提供条件逻辑。这需要一点“对象”的思考。所有场景都需要一致的方式来更新和绘制。

如果您的世界需要一段时间来加载,您可能希望显示一个临时的“世界正在加载”场景,这样您的用户就不会感到不耐烦。

于 2013-03-05T17:50:02.660 回答