function love.update (dt)
if love.keyboard.isDown("d") then
x = x +1
y = y + 1
end
if love.keyboard.isDown("a") then
x = x - 1
y = y - 1
end
if love.keyboard.isDown("escape") then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
Currently, when I press escape the window maximizes and then minimizes very quickly. Am I doing something wrong or is this the wrong way to go about it?
function love.keypressed(key, scancode, repeating)
if key == "escape" then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
end
function love.update (dt)
if love.keyboard.isDown("d") then
x = x +1
y = y + 1
end
if love.keyboard.isDown("a") then
x = x - 1
y = y - 1
end
if love.keyboard.isDown("escape") then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
Currently, when I press escape the window maximizes and then minimizes very quickly. Am I doing something wrong or is this the wrong way to go about it?
Your escape button is still down when you enter love.update(dt) the second time. It seems to be the wrong idea to use love.keyboard.isDown("escape") for this scenario. I would think about using love.keypressed() instead.
function toggleFullscreen()
local fs = love.window.getFullscreen()
fs = not fs
local rs = love.window.setFullscreen(fs, 'desktop')
return rs and fs or nil
end
Returns nil if the toggle fails or a Boolean if the mode was toggled successfully.
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
end
if fullscreen == true then
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
if not a then a = true end
if a then a = false end -- a will always be true here, because of the above, so it'll be set back to false -in the same frame-!
Which is short for toggling a twice, back to what it was.
Both the op's code and drunken_munki's code had this... so yeah, use an else instead:
function love.keypressed(key, scancode, repeating)
if key == "escape" then
if fullscreen == false then
love.window.setFullscreen(true, "desktop")
fullscreen = true
else
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
end
end
(Or use ivan's solution )
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.
-- In love.load(),
love.window.setFullscreen(false) -- <- change this to true if you want your game to be fullscreen when you run
fullscreen = false -- <- change this to true if you want your game to be fullscreen when you run
-- In love.update(),
-- This is the important stuff, I kinda don't wanna explain but it works sooooo,
-- Fullscreen / Not Fullscreen
if fullscreen == true then
love.window.setFullscreen(true)
elseif fullscreen == false then
love.window.setFullscreen(false)
end
function love.keypressed(k)
if k == "escape" and fullscreen == false then
fullscreen = true
elseif k == "escape" and fullscreen == true then
fullscreen = false
end
end
Last edited by UsualBasementKid on Sun Jun 19, 2022 12:37 pm, edited 1 time in total.
-- In love.load(),
love.window.setFullscreen(false) -- <- change this to true if you want your game to be fullscreen when you run
fullscreen = false -- <- change this to true if you want your game to be fullscreen when you run
-- In love.update(),
-- This is the important stuff, I kinda don't wanna explain but it works sooooo,
-- Fullscreen / Not Fullscreen
if fullscreen == true then
love.window.setFullscreen(true)
elseif fullscreen == false then
love.window.setFullscreen(false)
end
function love.keypressed(k)
if k == "escape" and fullscreen == false then
fullscreen = true
elseif k == "escape" and fullscreen == true then
fullscreen = false
end
end
Huh, why you check for key escape 2 times?
Why you elseif when you can just else?
And why not wrap it in function, so you can call this outside of love keypress?
And, most important, why necropost on like 6 year post?
local fullscreen = love.window.getFullscreen()
love.keyreleased = function(key, _)
if key == "escape" then
fullscreen = not fullscreen
love.window.setFullscreen(fullscreen, "desktop")
end
end
Fullscreen = love.window.getFullscreen() -- global
function love.keypressed (key, scancode) -- faster than key released
if key == "f11"
and love.keyboard.isDown( "rctrl", "lctrl" )
and not love.keyboard.isDown( "lalt", "ralt", "lshift", "rshift") then
Fullscreen = not Fullscreen
love.window.setFullscreen(Fullscreen, "desktop")
end
end
The best solution! But set the Fullscreen as global and set fullscreen on Ctrl + F11, not on Escape
function toggleFullscreen()
love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
updateDrawScaling()
end
It's triggered by a callback in my UI code, which its self is ultimately triggered by keypressed/keyreleased events. updateDrawScaling just figures out how much the drawing canvas must be scaled to fill the new window and it handles the case when the aspect ratios don't match (it's pretty inelegant though, and is driven by the window width ignoring the height).