Page 1 of 1
[SOLVED]Platformer help using multiple platforms
Posted: Sun Apr 17, 2016 10:36 pm
by Vimm
So I made a little platformer thingy with only one platform, it moves and jumps just fine, but as soon as i went to make another platform I knew I was doing it wrong.
Instead of trying to explain in detail, just try running the game and you'll see what I mean.
Re: Platformer help using multiple platforms
Posted: Mon Apr 18, 2016 11:15 pm
by unixfreak
The first thing i would do is put all platforms into a single table, and loop over them
For example in your love.load() do something like this:
Code: Select all
ground = { }
table.insert(ground, {
x = 0,
y = 500,
width = love.window.getWidth(),
height = 32
}
)
table.insert(ground, {
x = 0,
y = 400,
width = 128,
height = 32
}
)
That way you can loop over all platforms;
Example for love.update()
Code: Select all
for i, g in ipairs(ground) do
if collision(player.x, player.y, player.width, player.height, g.x, g.y, g.width, g.height) then
--put your collision code here
end
end
end
Then you can do the same for love.draw() aswell:
Code: Select all
for i, g in ipairs(ground) do
love.graphics.rectangle("fill", g.x, g.y, g.width, g.height)
end
Re: Platformer help using multiple platforms
Posted: Mon Apr 18, 2016 11:47 pm
by Vimm
unixfreak wrote:The first thing i would do is put all platforms into a single table, and loop over them
For example in your love.load() do something like this:
Code: Select all
ground = { }
table.insert(ground, {
x = 0,
y = 500,
width = love.window.getWidth(),
height = 32
}
)
table.insert(ground, {
x = 0,
y = 400,
width = 128,
height = 32
}
)
That way you can loop over all platforms;
Example for love.update()
Code: Select all
for i, g in ipairs(ground) do
if collision(player.x, player.y, player.width, player.height, g.x, g.y, g.width, g.height) then
--put your collision code here
end
end
end
Then you can do the same for love.draw() aswell:
Code: Select all
for i, g in ipairs(ground) do
love.graphics.rectangle("fill", g.x, g.y, g.width, g.height)
end
Ohh ok that makes sense, dang, why can't i think of these things on my own XD thanks!