[SOLVED]Platformer help using multiple platforms

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
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

[SOLVED]Platformer help using multiple platforms

Post 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.
Attachments
imBadAtMakingPlatformers.love
(658 Bytes) Downloaded 72 times
Last edited by Vimm on Mon Apr 18, 2016 11:47 pm, edited 1 time in total.
User avatar
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: Platformer help using multiple platforms

Post 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
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Platformer help using multiple platforms

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 223 guests