在 Love2D 中调用(导入)main.lua 之外的其他 .lua 文件
Calling (importing) other .lua files outside main.lua in Love2D
所以,我想通过将代码拆分成单独的文件来清理我的代码。但由于某种原因,我无法通过 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
关于这个话题我到处搜索,其他答案不是非常模糊就是过时了。我只想在开始爱的时候splash.lua运行
将您的 require
命令更改为以下内容应该有效:
require("splash")
此外,您应该在为上述 table.
创建函数之前创建 splash
table
splash = {}
splash.myFunction()
.
.
.
我注意到有两个问题,并且在我修复它们之后 运行。
问题 1
在main.lua中,require "splash.lua"
应该是require "splash"
。如果您使用 python、java 或 javascript 等其他语言编写代码,您可以将 require 视为类似于导入。
问题2
在 splash.lua 中,您引用了一个不存在的对象 (splash)。为了使您的代码尽可能相似,我在 splash.lua 的顶部插入了行 splash = {}
。创建对象 splash
后,您就可以为该对象创建函数(splash.load()、splash.update() 和 splash.draw())。这在 main.lua 中不是问题,因为 love
是 love2d 游戏引擎启动时已经存在的对象。
main.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.load
、splash.update
和 splash.draw
。从技术上讲,您可以随意命名要调用的任何函数,我只是觉得您可能认为这些加载、更新和绘制函数可能会被自动调用,但事实并非如此。 Love2d 只为 love
对象自动调用加载、更新和绘制函数。
所以,我想通过将代码拆分成单独的文件来清理我的代码。但由于某种原因,我无法通过 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
关于这个话题我到处搜索,其他答案不是非常模糊就是过时了。我只想在开始爱的时候splash.lua运行
将您的 require
命令更改为以下内容应该有效:
require("splash")
此外,您应该在为上述 table.
创建函数之前创建splash
table
splash = {}
splash.myFunction()
.
.
.
我注意到有两个问题,并且在我修复它们之后 运行。
问题 1
在main.lua中,require "splash.lua"
应该是require "splash"
。如果您使用 python、java 或 javascript 等其他语言编写代码,您可以将 require 视为类似于导入。
问题2
在 splash.lua 中,您引用了一个不存在的对象 (splash)。为了使您的代码尽可能相似,我在 splash.lua 的顶部插入了行 splash = {}
。创建对象 splash
后,您就可以为该对象创建函数(splash.load()、splash.update() 和 splash.draw())。这在 main.lua 中不是问题,因为 love
是 love2d 游戏引擎启动时已经存在的对象。
main.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.load
、splash.update
和 splash.draw
。从技术上讲,您可以随意命名要调用的任何函数,我只是觉得您可能认为这些加载、更新和绘制函数可能会被自动调用,但事实并非如此。 Love2d 只为 love
对象自动调用加载、更新和绘制函数。