错误 main.lua:45:尝试调用全局变量 'distanceFormula'(值为 nil)

我一直都会遇到这个错误,不管怎么样:

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

我很确定这可能与函数位置或其他东西有关。有人可以帮帮我吗?

最重要的是,有人可以帮我防止将来出现这样的错误吗?谢谢。

点赞
用户6331353
用户6331353

看起来你没有一个叫做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
2020-07-09 22:33:35