Disclaimer: I only recommend releasing and reloading resources like that when it actually takes up a lot of RAM, I don't think 60MB is really that much to keep in memory all the time.
You'll want to use two functions per state, like this:
function enter_lvl()
blue_sun = newAnimation(love.graphics.newImage ("/images/blue_sun.png"), 512, 512, 0.1, 20)
end
function leave_lvl()
blue_sun = nil
end
function draw_sun ()
blue_sun:draw (400, 300)
end
require("lvl")
[... snip ...]
function love.mousepressed(x, y, button )
if game_mode == "menu" then
if button == "l" then
game_mode = "space"
enter_lvl()
end
end
if game_mode == "space" then
if button == "r" then
leave_lvl()
game_mode = "menu"
end
end
end
There are libraries you can use that make using states a bit more structured.
Disclaimer: I only recommend releasing and reloading resources like that when it actually takes up a lot of RAM, I don't think 60MB is really that much to keep in memory all the time.
My game takes ~200 RAM and it no more 20% out of all graphic resources... So that, it very need for me.
There are libraries you can use that make using states a bit more structured.
In what sense?
They can do all the bookkeeping you now have to do manually (like calling enter_lvl and leave_lvl) automatically, as well as handle some more advanced features like state stacking, etc. I recommend using such a library for every game that's going to have more than two, three states.
Also, how much RAM does it use? 200MB? That's a lot, but quite doable on modern computers. Anyway, it's always a trade off, and this is your call.