Error whit loading sprites

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Matema
Prole
Posts: 1
Joined: Sun Mar 02, 2025 6:57 pm

Error whit loading sprites

Post by Matema »

This is the file game.lua:

Code: Select all

local game = {}
local gravity = 100
local score = 0

function game:load()
    arrow = {
        Arrowx = 380,
        Arrowy = 490,
        arrow1 = love.graphics.newImage("assets\arrow.png")
    }
    --enemy = {}
    love.graphics.setBackgroundColor(0, 0, 0)
    love.graphics.setDefaultFilter("nearest", "nearest")
    enemyx = love.math.random(0, 770)--mettere sempre 770
    enemyy = 60 --mettere sempre 60
    enemy.sprite = love.graphics.newImage('assets/blocktest.png')
end

function game:update(dt)
    if love.keyboard.isDown("d") then
        arrowx = arrowx + 10
    end
    if love.keyboard.isDown("a") then
        arrowx = arrowx - 10
    end
end

function game:draw()
    love.graphics.setLineWidth(5)    
    love.graphics.line(0, 540, 800, 540)
    love.graphics.line(0, 480, 800, 480) 
    --love.graphics.polygon("fill", 400, 540, 410, 520, 390, 520)
    love.graphics.draw(arrow.arrow1, arrow.Arrowx, arrow.Arrowy, 0, 2.5, 2.5)
    --love.graphics.draw(enemy, enemyx, enemyy, 0, 1, 1)
   -- love.graphics.print(score, 0, 80)
end

return game
This is the other file menu.lua:

Code: Select all

local menu = {}
local font = love.graphics.newFont("assets/PressStart2P-Regular.ttf", 50)
local Delta = "ΔDelta"
local Start = "Start"
local Exit = "Exit"
local Game = require("game")

function menu.draw()
    love.graphics.setFont(font)
    love.graphics.print(Delta, 0, 300)
    love.graphics.print(Start, 0, 400)
    love.graphics.print(Exit, 0, 500)
end

function menu.keyreleased(key)
        if key == "w" then
            Start = "• Start"
            Exit = "Exit"
        end
        if key == "s" then
            Exit = "• Exit"
            Start = "Start"
        end
        if key == "return" then
            if Start == "• Start" then
                function love.load()
                    Game.load()
                end
                function love.draw()
                    Game.draw()
                end
            end
            if Exit == "• Exit" then
                love.event.quit()
            end
        end
end

return menu
The final file called main.lua:

Code: Select all

function love.load()
    Menu = require("menu")
    Game = require("game")
end

function love.draw()
    Menu.draw()
end

function love.keyreleased(key)
    Menu.keyreleased(key)
end
This is the error that gives me:
Image
Attachments
Screenshot 2025-03-02 200004.png
Screenshot 2025-03-02 200004.png (20.87 KiB) Viewed 1366 times
User avatar
dusoft
Party member
Posts: 792
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Error whit loading sprites

Post by dusoft »

Weird naming conventions with lowercase/uppercase, but I would suspect the game:load is not run on load... Test with printing a message or similar debug.
RNavega
Party member
Posts: 465
Joined: Sun Aug 16, 2020 1:28 pm

Re: Error whit loading sprites

Post by RNavega »

Hi. If I were in your position I'd focus on merging all scripts into a single big script (main.lua) and then work just with that for the time being, until I have more experience with the framework and can expand to other files.

The actual error in your program comes from a misunderstanding of how love.load is used by the LÖVE framework: it's called once after main.lua is parsed and then it's never called again during the program. So if you redefine that function like you're doing in menu.keyreleased(), it still won't be called.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests