Why can I only jump once?

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
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Why can I only jump once?

Post by onedaysnotice »

I finally got jumping and gravity to work, but now I've got another problem to face. I can only jump once! Dx I have no idea as to why this is happening. Help pls? :(

Code: Select all

debug = false

function love.load()
	player = {}
	player.y = 500
	player.x = 100
	player.w = 50
	player.h = 50
	player.b = player.y + player.h
	player.r = player.x + player.w
	player.y_vel = -600
	player.state = "idle"

	ground = {}
	ground.y = 550
	ground.w = love.graphics.getWidth()
	ground.h = love.graphics.getHeight() - ground.y

	presskey = "none"
	releasekey = "none"
	gravity = 1000
end

function love.update(dt)
	-- if love.keyboard.isDown("up") and not player.state == "jumping" then
	if presskey == "up" then
		player.state = "jumping"
	end

	if player.state == "jumping" then
		player.y = player.y + player.y_vel * dt
		player.y_vel = player.y_vel + gravity * dt
		if player.y + player.h > ground.y then
			player.y = ground.y - player.h
			player.state = "idle"
			presskey = "none"
		end
	end

end

function love.draw()
	love.graphics.rectangle("fill",player.x,player.y,player.w,player.h)
	love.graphics.rectangle("fill",0,ground.y,ground.w,ground.h)
	-- if debug then
	love.graphics.print(player.state,50,50,0,1,1)
	love.graphics.print(player.y,50,60,0,1,1)
	love.graphics.print(presskey,50,70,0,1,1)
	-- end
end

function love.keypressed(key)

	if key == "up" then
		presskey = "up"
	end
end

function love.keyreleased(key)
	if key == "`" then
		debug = not debug
	end

	if key == "up" then
		releasekey = "up"
	end
end
thanks.
Attachments
test.love
(961 Bytes) Downloaded 80 times
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Why can I only jump once?

Post by Lynesth »

Hi !

It seems taht you simply forgot to reset player.y_vel to -600 when player is back in idle state.
Which means that you just have to do that :

Code: Select all

if player.state == "jumping" then
      player.y = player.y + player.y_vel * dt
      player.y_vel = player.y_vel + gravity * dt
      if player.y + player.h > ground.y then
         player.y = ground.y - player.h

         -- Here is what you should add !! --
         player.y_vel = -600
         -- That's all ;) --

         player.state = "idle"
         presskey = "none"
      end
   end
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Why can I only jump once?

Post by coffee »

Lynesth wrote: It seems taht you simply forgot to reset player.y_vel to -600 when player is back in idle state.
Yeah but I'm personally not a fan that hardcoded value and it's not great that he put fixed values inside the jumping routine so I suggest some simple changes.
I simplified the key routines and cleaned not needed variables like "presskey"/"releasekey". I think it's simpler now.

Some other optimizations could be made. I would also make a simpler player.jumping Boolean instead of player.state "idle"/"jumping" but I didn't want change too much.

Code: Select all

debug = true

function love.load()

	ground = {}
	ground.y = 550
	ground.w = love.graphics.getWidth()
	ground.h = love.graphics.getHeight() - ground.y

	player = {}
	player.y = 500
	player.x = 100
	player.w = 50
	player.h = 50
	player.b = player.y + player.h
	player.r = player.x + player.w
	player.y_vel_base = -(ground.y + player.h) 
	player.y_vel = player.y_vel_base
	player.state = "idle"

	gravity = 1000
end

function love.update(dt)

	if love.keyboard.isDown("up") and player.state == "idle" then
		player.state = "jumping"
	end

	if player.state == "jumping" then
		player.y = player.y + player.y_vel * dt
		player.y_vel = player.y_vel + gravity * dt
		if player.y + player.h > ground.y then
			player.y = ground.y - player.h
			player.y_vel = player.y_vel_base
			player.state = "idle"
		end
	end

end

function love.draw()
	love.graphics.rectangle("fill",player.x,player.y,player.w,player.h)
	love.graphics.rectangle("fill",0,ground.y,ground.w,ground.h)
	if debug then
		love.graphics.print(player.state,50,50,0,1,1)
		love.graphics.print(player.y,50,60,0,1,1)
	end
end

function love.keypressed(key)

end

function love.keyreleased(key)
	if key == "tab" then
		debug = not debug
	end
end
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: Why can I only jump once?

Post by onedaysnotice »

Thanks Lynesth and coffee! :D
coffee wrote:Some other optimizations could be made. I would also make a simpler player.jumping Boolean instead of player.state "idle"/"jumping" but I didn't want change too much.
I'm going to add other states like running, attacking and blocking, so I thought I might as well just put them all into one variable. :D

also, do you know why if I change player.y + player.h to player.b, that the collision doesn't happen anymore? :S

Code: Select all

 if player.state == "jumping" then
      player.y = player.y + player.y_vel * dt
      player.y_vel = player.y_vel + gravity * dt
      if player.y + player.h > ground.y then <=== insert player.b instead
         player.y = ground.y - player.h
         player.y_vel = player.y_vel_base
         player.state = "idle"
      end
   end
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Why can I only jump once?

Post by coffee »

onedaysnotice wrote: I'm going to add other states like running, attacking and blocking, so I thought I might as well just put them all into one variable. :D
Yes, but that is the problem and you didn't notice the issues you get in.
So let's examine it.
image that your player is not jumping but also isn't idle or quiet like is moving to right. Does player player.status = "idle" make sense? No. So you change to player.status = "walking_right" Now imagine that you are "walking_right" but you jump too! Ops! Since your player is doing simultaneous things you can't use player.status for different things. Or you keep "walking_right" or you loose your "jump" detection. See the problem?
I would suggest

Code: Select all

player.status = {
  jumping = false,
  facing = "right",
  walking = false,
...
}
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Why can I only jump once?

Post by Lynesth »

onedaysnotice wrote:also, do you know why if I change player.y + player.h to player.b, that the collision doesn't happen anymore? :S
Do you update player.b each time player.y changes ? If you don't, then I think that might just be the thing to do :)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: Why can I only jump once?

Post by onedaysnotice »

@coffee @Lynesth Oops. didn't of that. thanks. :D Changed it :)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 200 guests