1
votes

I keep getting this error, no matter what:

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

Can someone help me? The error goes like this:

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

I'm very sure that it probably has to do with the function place or something. Can someone help me?

Most importantly can someone please help me with preventing future errors like this??? Thank you.

1

1 Answers

2
votes

It looks like you don't have a function called distanceFormula. Also I don't know why you are declaring your distanceBetween function inside of love.mousepressed. distanceFormula is "a nil value" because it doesn't exist. The error is referring to line 45 in your code

if distanceFormula(button.x, button.y, love.mouse.getX(), love.mouse.getY()) < button.size then

I changed your call to distanceFormula to be a call to distanceBetween and moved distance between to it's own function. I also had to move a few of your end around and got rid of your extra 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