错误 main.lua:45:尝试调用全局变量 'distanceFormula'(值为 nil)
2020-7-10 12:5:12
收藏:0
阅读:90
评论:1
我一直都会遇到这个错误,不管怎么样:
function love.load() -- love.load 开始任何命令
--number = 0 (旧代码)
button = {}
button.x = 200
button.y = 200
button.size = 50
button.score = 0
button.time = 0
newFont = love.graphics.newFont(40)
end
end
function love.update(dt) -- 在delta time中更改代码:每帧:love会以每秒60帧的速度运行
--number = number + 1 (old code)
end
function love.draw() -- 在屏幕上做任何事情
end
function love.draw()
--love.graphics.setColor(0, 0, 255, 5)
--love.graphics.rectangle("填充", 200, 400, 200, 100) --love.graphics.rectangle(模式、x、y、width、height)Y向下增加;
-- width increases to the right and the height increases downward assuming positive numbers
love.graphics.setColor(255,0,0,5)
--love.graphics.print(number) (old code)
--love.graphics.circle("fill", 150, 350, 100)
--love.graphics.circle(mode, x, y, radius, segments)
love.graphics.circle("fill", button.x, button.y, button.size)
love.graphics.setFont(newFont)
love.graphics.setColor(255, 255, 255, 1)
love.graphics.print(button.score)
end
function love.mousepressed( x, y, b, isTouch)
if b == 1 then
-- 使用距离公式来测量距离/n!
if distanceFormula(button.x, button.y, love.mouse.getX(), love.mouse.getY()) < button.size then
--检查单击是否在圆内。
score = score + 1
end
end
function distanceBetween(x1,y1,x2,y2)
return math.sqrt((y2-y1)^2 + (x2-x1)^2)
end
有人可以帮帮我吗? 错误如下:
Error main.lua:45: attempt to call global 'distanceFormula' (a nil value)
Traceback
main.lua:45: in function <main.lua:42>
[C]: in function 'xpcall
我很确定这可能与函数位置或其他东西有关。有人可以帮帮我吗?
最重要的是,有人可以帮我防止将来出现这样的错误吗?谢谢。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

看起来你没有一个叫做
distanceFormula的函数。并且我不知道为什么你正在love.mousepressed内部声明你的distanceBetween函数。distanceFormula是“空值”,因为它不存在。错误指到你代码的第45行。if distanceFormula(button.x, button.y, love.mouse.getX(), love.mouse.getY()) < button.size then我将你对
distanceFormula的调用更改为对distanceBetween的调用,并将距离移动到了它自己的函数中。我还移动了一些你的end并删除了你多余的love.draw()。function love.load() --number = 0 (old code) button = {} button.x = 200 button.y = 200 button.size = 50 button.score = 0 button.time = 0 newFont = love.graphics.newFont(40) end function love.update(dt) -- changes code in delta time: every frame: love runs in 60 frames per second --number = number + 1 (old code) end function love.draw() --love.graphics.setColor(0, 0, 255, 5) --love.graphics.rectangle("fill", 200, 400, 200, 100) --love.graphics.rectangle(mode, x, y, width, height) Y increases downwards; -- width increases to the right and the height increases downward assuming positive numbers love.graphics.setColor(255,0,0,5) --love.graphics.print(number) (old code) --love.graphics.circle("fill", 150, 350, 100) --love.graphics.circle(mode, x, y, radius, segments) love.graphics.circle("fill", button.x, button.y, button.size) love.graphics.setFont(newFont) love.graphics.setColor(255, 255, 255, 1) love.graphics.print(button.score) end function love.mousepressed(x, y, b, isTouch) if b == 1 then -- use the distance formula to measure the distance/n! if distanceBetween(button.x, button.y, love.mouse.getX(), love.mouse.getY()) < button.size then --checks whether click is in circle. button.score = button.score + 1 end end end function distanceBetween(x1,y1,x2,y2) return math.sqrt((y2-y1)^2 + (x2-x1)^2) end