My game keeps crashing every time I want to run it
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 1
- Joined: Tue Nov 19, 2019 12:47 pm
My game keeps crashing every time I want to run it
Hi, so I'm new to Love, and I am trying to make a countdown to a certain date. In it, I have loops, which I suspect are the problem, but I can't make the program run without it. Attatched is my love file. Any help would be appreciated.
- Attachments
-
- ticktock.love
- (128.12 KiB) Downloaded 132 times
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: My game keeps crashing every time I want to run it
Hi and welcome to the forums!
The mistakes i have found:
- calling os.execute was pointless in that you can just print to the console if you have it enabled in conf.lua or you're running this with lovec.exe;
- löve already has a loop, so define infinite or timing loops can get the application to hang that way; use the deltatime you get in love.update or from love.timer.getDelta;
- ticktock was local to the dispTime function, meaning you could not print it in love.draw
- love.love.audio.stop is not something that exists, you probably wanted love.audio.stop but even that's unnecessary; löve will stop all sounds when it quits anyway.
I did implement a working version, but do try to learn from it.
The mistakes i have found:
- calling os.execute was pointless in that you can just print to the console if you have it enabled in conf.lua or you're running this with lovec.exe;
- löve already has a loop, so define infinite or timing loops can get the application to hang that way; use the deltatime you get in love.update or from love.timer.getDelta;
- ticktock was local to the dispTime function, meaning you could not print it in love.draw
- love.love.audio.stop is not something that exists, you probably wanted love.audio.stop but even that's unnecessary; löve will stop all sounds when it quits anyway.
I did implement a working version, but do try to learn from it.
Code: Select all
function dispTime(t)
local d = math.floor( t / 86400)
local h = math.floor((t % 86400) / 3600)
local m = math.floor((t % 3600) / 60)
local s = math.floor( t % 60)
return ("%d:%02d:%02d:%02d"):format(d, h, m, s)
end
local countdown = {year = 2020, month = 11, day = 15}
local remaining
function love.load()
remaining = os.difftime(os.time(countdown), os.time())
tick = love.audio.newSource("tick.mp3", "static")
abweb = love.graphics.newFont("abweb.ttf", 20)
pic = love.graphics.newImage('unus annus.jpg')
end
local second_timer = 0.0
local even = false
function love.update(dt)
-- add elapsed time to accumulator
second_timer = second_timer + dt
-- if a second has elapsed...
if second_timer >= 1.0 then
-- progress countdown
remaining = os.difftime(os.time(countdown), os.time())
-- play clock tick sound every second;
-- the seek methods are only there because we're using one Source object, they're probably not that important anyway and could be removed...
if even then
tick:setPitch(0.8) -- lower pitch
tick:seek(0)
tick:play()
else
tick:setPitch(1.0)
tick:seek(0)
tick:play()
end
-- even and odd ticks sound different for aesthetic purposes.
even = not even
-- reset accumulator while keeping the remainder
second_timer = second_timer % 1.0
end
end
function love.draw()
local W,H = love.graphics.getDimensions()
local w,h = pic:getDimensions()
love.graphics.setColor(1,1,1,.5)
love.graphics.draw(pic, W/2-w/2, H/2-h/2)
love.graphics.setColor(1,1,1,1)
love.graphics.print("Press Escape to quit.",0,0)
love.graphics.printf(dispTime(remaining), abweb, 0, H/2-abweb:getHeight()/2, W, 'center')
end
function love.keypressed(k)
if k == 'escape' then
love.event.quit()
end
end
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Who is online
Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 2 guests