love2d - 在 Love2D 中调用(导入)main.lua 之外的其他 .lua 文件
问题描述
因此,我想通过将代码拆分为单独的文件来清理我的代码。但由于某种原因,我无法通过 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 运行。
解决方案
我注意到了两个问题,一旦我修复了它们,就会出现问题。
问题 1
在 main.lua 中,require "splash.lua"
应该是require "splash"
. 如果你用 Python、java 或 javascript 等其他语言编写代码,你可以认为 require 类似于 import。
问题 2
在 splash.lua 中,您正在引用一个不存在的对象(splash)。为了使您的代码尽可能地相似,我splash = {}
在 splash.lua 的顶部插入了这一行。创建对象splash
后,您就可以为此对象创建函数(splash.load()、splash.update() 和 splash.draw())。这在 main.lua 中不是问题,因为love
它是一个在 love2d 游戏引擎启动时已经存在的对象。
主程序.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
飞溅.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
. 从技术上讲,您可以命名您想要调用的任何函数,我只是感觉您可能认为这些加载、更新和绘制函数可能会被自动调用,但事实并非如此。love
Love2d 只为对象自动调用加载、更新和绘制函数。
推荐阅读
- c - 如何使用 gnuplot 将 4d 数据绘制到热图中
- windows - 如何使此 Windows 脚本跳过现有文件?
- visual-studio - 将 DLL 依赖文件复制到主项目
- react-native - 我正在尝试使用 React Ntaive FlatList 和 pagingEnabled 创建像 TikTok 这样的幻灯片,但添加了自动上边距,谁能帮助我
- python - 如何在带有 OR 条件的 re.search 中使用多个字符串?
- rust - 如何通过期货通道将异步消息发送到接收器?
- php - 将一串逗号分隔值转换为真/假数组
- react-native - React Native:在本地服务器上使用 axios 时出现网络错误
- javascript - 在 JSON 请求中发送 csrf_token(无 ajax)
- mongodb - 查找 mongo 文档 - 具有嵌套字符串数组的文档数组