Load levels in a platformer

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Gurkani
Prole
Posts: 5
Joined: Sun Apr 27, 2014 3:53 pm

Load levels in a platformer

Post by Gurkani »

I'm doing a platformer and I'm trying to do a loading system for levels, but I have a problem, there is the part of my code :

Code: Select all

--main.lua
require "level1" ; require "level2"

function love.load()
	        	love.physics.setMeter(64)
	        	world = love.physics.newWorld(0,9.81*64, true)
	--[...]
	level = 1
	if level == 1 then load_level1(world)
	elseif level == 2 then unload_level1() ; load_level2(world) end
end

function love.draw()
	if level == 1 then draw_level1()
	elseif level == 2 then draw_level2() end
end

function love.update()
	--my code
end

Code: Select all

--level1.lua
function load_level1(world)
obj1 = {}
obj1.body = love.physics.newBody(world, 111,111, "dynamic")
obj1.shape = love.physics.newRectangleShape(28,28)
obj1.fixture = love.physics.newFixture(obj1.body,obj1.shape)
end
function unload_level1()
obj1 = nil
end
function draw_level1()
	love.graphics.polygon("line", obj1.body:getWorldPoints(obj1.shape:getPoints()))
end
level2.lua it's the same but with another rectangle, and with functions like "load_level2"

The problem is that I can't check the level variable in the love.load() function because it's called just one time. Is there another solution ?
User avatar
Foxcraft
Prole
Posts: 49
Joined: Sat Mar 22, 2014 8:56 pm

Re: Load levels in a platformer

Post by Foxcraft »

Hello Gurkani,

Yes, love.load only does it the one time. It's like your pre-game running stage, not too different than how you have to gather your ingredients together first before actually starting to follow a recipe.

Then the game loop kicks in. This goes through events (like love.keypressed, love.resize, etc.), then love.update, then love.draw.

So you load, then events, update, draw. Events, update, draw. Events, update, draw. That's the basic cycle.

Anything that doesn't have to do with directly drawing something, you put in love.update. So, you should put that level check into love.update.

However, there is a problem in your setup. As it is, if you just move it to love.update, so long as it's level 1, it's written to keep loading up level 1. Over and over. So long as it's level 2, keep unloading level 1 and load up level 2. Over and over. But you really only want to do that the one time (and maybe that's why you thought to put it in love.load instead?).

You'll want to use some sort of game states here. If...*thinks up names*...gamestate == "normal", do nothing special. If gamestate=="nextLevel" and the level is 1, unload level 1, load up level 2, increment the level number, and change the gamestate to normal.

You just have to change what the game state is when you want to trigger going to the next level instead of just incrementing the level number yourself.

(You probably won't want to put in a check for something like gamestate == "normal" if you really have nothing to do there, instead just checking if the game state is something else. I mostly put it there for the train of thought.)
Gurkani
Prole
Posts: 5
Joined: Sun Apr 27, 2014 3:53 pm

Re: Load levels in a platformer

Post by Gurkani »

Ok so I tried this :

Code: Select all

    --main.lua
    require "level1" ; require "level2"
    function love.load()
                  love.physics.setMeter(64)
                  world = love.physics.newWorld(0,9.81*64, true)
       --[...]
       level = 1
    end

    function love.draw()
       if level == 1 then draw_level1()
       elseif level == 2 then draw_level2() end
    end

    function love.update(dt)
       world:update(dt)
       if gs == "loading2" then unload_level2() ; load_level2() end
       --[...]
     end

function love.keypressed(key)
	if key == "u" then level = 2 gs = "loading2" else gs = "normal" end
end
And the level1 and level2 files; it's the same.

But now when I press u button, the objects disappear, but their body is still there (When the player touch them, the collision happens but they are invisible) How can I do ?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests