【问题标题】:Calling (importing) other .lua files outside main.lua in Love2D在 Love2D 中调用(导入)main.lua 之外的其他 .lua 文件
【发布时间】:2019-07-23 20:58:37
【问题描述】:

所以,我想通过将代码拆分为单独的文件来清理我的代码。但由于某种原因,我无法通过 main.lua 调用我想调用的脚本。

这是我的 main.lua 脚本:

function love.load()
  require "splash.lua"
  splash.load()
end

function love.update(dt)
  splash.update(dt)

end

function love.draw() print("Draw")
  splash.draw()
  love.graphics.print("FPS "..tostring(love.timer.getFPS( )), 5, 5) print("fps")
  love.graphics.setColor(255,0,0) --Red
  love.graphics.rectangle("fill", 3, 3, 60, 20)
end

这是我的 splash.lua 脚本,与 main.lua 文件分开:

function splash.load()
    timer = 0 print("fadein")
    alpha = 0
    fadein  = 2
    display = 4
    fadeout = 6
    splashScreen = love.graphics.newImage("/images/Splash1.png")
end

function splash.update(dt)
    timer = timer + dt
    if 0 < timer and timer < fadein then
        alpha = timer / fadein
    end
    if fadein < timer and timer < display then
        alpha = 1
    end
    if display < timer and timer < fadeout then
        alpha = 1 - ((timer - display) / (fadeout - display))
    end
end

function splash.draw()
    love.graphics.setColor(1, 1, 1, alpha)
    local sx = love.graphics.getWidth() / splashScreen:getWidth()
    local sy = love.graphics.getHeight() / splashScreen:getHeight()
    love.graphics.draw(splashScreen, 0, 0, 0, sx, sy)
end

我在这个主题上到处搜索,其他答案要么非常模糊,要么已经过时。我想做的就是在启动 Love 时让 splash.lua 运行。

【问题讨论】:

    标签: love2d


    【解决方案1】:

    我注意到了两个问题,一旦我修复了它们,就会出现问题。

    问题 1 在 main.lua 中,require "splash.lua" 应该是 require "splash"。如果你用 Python、java 或 javascript 等其他语言编写代码,你可以认为 require 类似于 import。

    问题 2 在 splash.lua 中,您正在引用一个不存在的对象(splash)。为了使您的代码尽可能地相似,我在 splash.lua 的顶部插入了 splash = {} 行。创建对象splash 后,您就可以为此对象创建函数(splash.load()、splash.update() 和 splash.draw())。这在 main.lua 中不是问题,因为 love 是一个在 love2d 游戏引擎启动时已经存在的对象。

    ma​​in.lua

    function love.load()
      require "splash"
      splash.load()
    end
    
    function love.update(dt)
      splash.update(dt)
    end
    
    function love.draw() print("Draw")
      splash.draw()
      love.graphics.print("FPS "..tostring(love.timer.getFPS( )), 5, 5) print("fps")
      love.graphics.setColor(255,0,0) --Red
      love.graphics.rectangle("fill", 3, 3, 60, 20)
    end
    

    splash.lua

    splash = {}
    
    function splash.load()
        timer = 0 print("fadein")
        alpha = 0
        fadein  = 2
        display = 4
        fadeout = 6
        splashScreen = love.graphics.newImage("/images/Splash1.png")
    end
    
    function splash.update(dt)
        timer = timer + dt
        if 0 < timer and timer < fadein then
            alpha = timer / fadein
        end
        if fadein < timer and timer < display then
            alpha = 1
        end
        if display < timer and timer < fadeout then
            alpha = 1 - ((timer - display) / (fadeout - display))
        end
    end
    
    function splash.draw()
        love.graphics.setColor(1, 1, 1, alpha)
        local sx = love.graphics.getWidth() / splashScreen:getWidth()
        local sy = love.graphics.getHeight() / splashScreen:getHeight()
        love.graphics.draw(splashScreen, 0, 0, 0, sx, sy)
    end
    

    旁注:通过阅读您的代码,我不确定您是否完全了解加载、更新和绘制函数的工作原理。 Love 是由 love2d 游戏引擎创建的对象。引擎只在游戏开始时运行一次加载函数。然后,更新函数不断检查游戏所在世界的变化,并根据其中的条件应用 love.update 中的代码。 draw 函数不断地一遍又一遍地绘制它被告知的内容。这些事情是自动发生的,因为它已经被编程到 love2d 游戏引擎中。 我认为您可能在这方面感到困惑的唯一原因是您创建了一个单独的splash.loadsplash.updatesplash.draw。从技术上讲,您可以命名您想要调用的任何函数,我只是感觉您可能认为这些加载、更新和绘制函数可能会被自动调用,但事实并非如此。 Love2d 只为love 对象自动调用加载、更新和绘制函数。

    【讨论】:

    • 嗨,Jacob,非常感谢您的回答,它运行良好。只是想与您澄清一下,我了解 love 对象是代码运行时唯一调用的对象。但这不仅适用于main.lua 吗?我假设 Love2D 只是忽略了 main.lua 之外的任何其他 love 调用。如果我错了,请随时纠正我。谢谢。
    • @Koto 是的,看起来你明白了,我只是不确定,因为你专门创建了其他 load updatedraw 函数。我相信它只存在于main.lua。我还认为这个解释可能有助于解释为什么无法调用任何启动函数。
    【解决方案2】:

    将您的 require 命令更改为以下内容应该可以:

    require("splash")
    

    此外,您应该在为所述表创建函数之前创建 splash 表。

    splash = {}
    
    splash.myFunction()
    .
    .
    .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-12
      • 2018-10-30
      • 2018-10-11
      • 2017-11-22
      • 2021-03-11
      • 1970-01-01
      • 2019-11-21
      • 2022-01-17
      相关资源
      最近更新 更多