wonky player controls

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

wonky player controls

Post by Vimm »

so I tried to see how far I could get in making a platformer on my own, with no references. I got a lot farther than I thought I would but I've hit a snag. My players controls are very..odd, try running it and you'll see what I mean. Unless its somehow just a me problem in which case...rip
Attachments
platformMovement.love
(596 Bytes) Downloaded 51 times
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: wonky player controls

Post by bobbyjones »

It does not run for me. What version of love is it for?
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: wonky player controls

Post by mr_happy »

I made a couple of edits just to make your code run on 0.10. I didn't look at it in detail so I'm not sure whether it does what you expect?

Code: Select all

function love.load()
	player = 
		{
			x = 100,
			y = 100,
			xvel = 0,
			yvel = 0,
			width = 32,
			height = 32,
			canFall = true
		}
		
		
	ground = 
		{
			x = 0,
			y = 500,
			w = love.graphics.getWidth(),
            h=32
			
		}
		gravity = 1034
end

function love.update(dt)
	if player.canFall then
		player.yvel = player.yvel + gravity * dt
	end
	player.y = player.y + player.yvel * dt
	player.x = player.x + player.xvel * dt
	
	if collision(player.x, player.y, player.width, player.height, ground.x, ground.y, ground.w, ground.h) then
		player.yvel = 0
		player.canFall = false
		player.y = player.y
	end
end

function love.keypressed(key)
	if not player.canFall then 
		if key == " " then
			player.yvel = -600
			player.canFall = true
		end
	end
	
	if key == "a" then
		player.xvel = -300
	elseif key == "d" then
		player.xvel = 300
	end
end

function love.keyreleased(key)
	if key == "a" then
		player.xvel = 0
	elseif key == "d" then
		player.xvel = 0
	end
end

function collision(x1,y1,w1,h1, x2,y2,w2,h2)
	return x1 < x2+w2 and
			x2 < x1+w1 and
			y1 < y2+h2 and
			y2 < y1+h1
end

function love.draw()
	love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
	
	love.graphics.rectangle("fill", ground.x, ground.y, ground.w, ground.h)
end

User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: wonky player controls

Post by Vimm »

bobbyjones wrote:It does not run for me. What version of love is it for?
I use 0.9.2
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: wonky player controls

Post by Vimm »

Vimm wrote:so I tried to see how far I could get in making a platformer on my own, with no references. I got a lot farther than I thought I would but I've hit a snag. My players controls are very..odd, try running it and you'll see what I mean. Unless its somehow just a me problem in which case...rip
Ok I figured it out, for some reason using love.keypressed() and love.keyreleased() was making it act weird, I had to switch to using love.keyboard.isDown() in the update function, then use keyreleased. the controls are fine now.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 227 guests