错误 main.lua:45:尝试调用全局 'distanceFormula'(零值)

Error main.lua:45: attempt to call global 'distanceFormula' (a nil value)

无论如何,我总是收到这个错误:

function love.load() -- love.load starts any command inthe beginning
    --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
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() -- does anything on the screen
    
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 distanceFormula(button.x, button.y, love.mouse.getX(), love.mouse.getY()) < button.size then
                --checks whether click is in circle.

                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)

回溯

main.lua:45: in function <main.lua:42>
[C]: in function 'xpcall

我很确定这可能与功能位置有关。有人可以帮助我吗?

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

您似乎没有名为 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