I can't make a character fall down...

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
NanoSpace
Prole
Posts: 7
Joined: Sat Feb 25, 2012 10:18 pm

I can't make a character fall down...

Post by NanoSpace »

I searched for some Physics tutorials but no luck. All I want to do is if you release the "W" key then the character falls down until it hits the ground. I know you would need some type of ground..and collision detection..and some velocity(I think)..but sadly I don't know much about that. Does anyone know a simpler way? :cry:
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: I can't make a character fall down...

Post by Puzzlem00n »

Well, this is an odd question, but lucky for you, I'm an odd person.

Code: Select all

function love.load()
	player = {
		x = 380,
		y = 180,
		grav = 0 -- gravity amount
		}
end

function love.keyreleased(key)
	if key == "w" then
		player.grav = 90 -- we make it a value to go down
		print(release)
	end
end

function love.update(dt)
	player.y = player.y + player.grav * dt -- We add gravity to the player's y position
	if player.y > 400 then -- Are we on the ground? (which we'll call 400 y)
		player.y = player.y - player.grav * dt -- If we are, We undo that moving down
	end
end

function love.draw()
		love.graphics.rectangle("fill", player.x, player.y, 40, 40) -- Draw it!
end
That should do exactly what you want.
I LÖVE, therefore I am.
Post Reply

Who is online

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