How can I make my character jump?

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.
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

How can I make my character jump?

Post by BEANSFTW » Sun Apr 15, 2012 9:44 pm

Hi. I'm working on a platformer game, but I can't figure out how to make my character jump.

Here's my code

Code: Select all



function love.load()

	
  
	player = love.graphics.newImage("assets/player.png")
 
	playerx = 270
  
	playery = 420
  
	bg = love.graphics.newImage("assets/backround.png")
  
	-- music
	music = love.audio.newSource( "assets/music.ogg" , "steam")
	music:setLooping(true)
	love.audio.play(music)
  
	Cloudx = 0
  
    yCloud = 128
  
	cloudimage = love.graphics.newImage("assets/cloud.gif")
	bullet = love.graphics.newImage("assets/bullet.gif")
	
	right = love.graphics.newImage("assets/player.png")
	left = love.graphics.newImage("assets/playerback.png")
  
	
end


function love.update(dt)
 


	Cloudx =  Cloudx + 32*dt 
	if Cloudx >= (900 + 278) then
		Cloudx = 0
	end

	
	
	if love.keyboard.isDown("d") then
		playerx = playerx + 275*dt
		player = right
	end	

	if love.keyboard.isDown("s") then
		playery = playery + 675*dt
	end
	
	
	
	if love.keyboard.isDown("a") then
		player = left
		playerx = playerx - 275*dt
	end	
	
	
	if playery > 420 then
		playery = 420
	end	
	
		
end

function love.draw()
  
	
	
	
	love.graphics.setColor(40,255,40)
    love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
    love.graphics.setColor(255,255,255)
  
  
    love.graphics.setColor(111,183,255)
	love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),450)
	love.graphics.setColor(255,255,255)
  
	
	
	love.graphics.draw(cloudimage, Cloudx - 278, 128)
	
	
	
	love.graphics.draw(player, playerx, playery)
end










User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: How can I make my character jump?

Post by Refpeuk » Sun Apr 15, 2012 10:15 pm

This is probably one of the most commonly asked mechanical questions on this forum. Try the handy search function.
It was the best of times, it was the worst of times . . .

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How can I make my character jump?

Post by tentus » Sun Apr 15, 2012 11:21 pm

Also, you can replace this:

Code: Select all

   love.graphics.setColor(40,255,40)
    love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
   love.graphics.setColor(255,255,255)
with this, inside of love.load:

Code: Select all

love.graphics.setBackgroundColor(40,255,40)
Kurosuke needs beta testers

User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: How can I make my character jump?

Post by Puzzlem00n » Sun Apr 15, 2012 11:28 pm

Well, I can give you my method of choice if you'd like.

Code: Select all

function love.load()
	player = {
		x = 280,
		y = 380,
		ySpeed = 0, -- the Speed we'll add to the Y movement
		gravSecond = 1.5 -- gravity amount
		}
end

function love.keypressed(key)
	if key == "up" and player.y < 400 then -- Are we on the ground?
		player.ySpeed = -20 -- Make us add a negative, to move up
	end
end

function love.update(dt)
	frame = dt * 30 -- I do this just because I think better this way
	player.ySpeed = player.ySpeed + player.gravSecond * frame -- We add gravity to the ySpeed
	player.y = player.y + player.ySpeed * frame -- We add ySpeed to the player's y position
	if player.y > 400 then -- Are we on the ground?
		player.y = player.y - player.ySpeed * frame -- If we are, We undo that moving down
		player.ySpeed = 0 -- The ySpeed is reset
	end
end



function love.draw()
		love.graphics.rectangle("fill", player.x, player.y, 40, 40) -- Draw it!
end
That make sense?
I LÖVE, therefore I am.

User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: How can I make my character jump?

Post by BEANSFTW » Mon Apr 16, 2012 12:46 am

I don't get it. Plus, Theres a problem where I can jump in mid air as much times as I want.

User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: How can I make my character jump?

Post by Qcode » Mon Apr 16, 2012 1:51 am

I can't really help you understand but I can add something to puzzlem00n's code to stop that bug.

All I do is make a variable like "inAir" and add it to the player table.

Code: Select all

function love.load()
   player = {
      x = 280,
      y = 380,
      ySpeed = 0, -- the Speed we'll add to the Y movement
      gravSecond = 1.5, -- gravity amount
      inAir = false
      }
end

Then after that set it so when he jumps the player.inAir = true

Code: Select all

function love.keypressed(key)
   if key == "up" and player.y < 400 and not player.inAir then -- Are we on the ground?
      player.ySpeed = -20 -- Make us add a negative, to move up
      player.inAir = true
   end
end
Then we also have to set if so that when we land we are not "inAir"

Code: Select all

function love.update(dt)
   frame = dt * 30 -- I do this just because I think better this way
   player.ySpeed = player.ySpeed + player.gravSecond * frame -- We add gravity to the ySpeed
   player.y = player.y + player.ySpeed * frame -- We add ySpeed to the player's y position
   if player.y > 400 then -- Are we on the ground?
      player.y = player.y - player.ySpeed * frame -- If we are, We undo that moving down
      player.ySpeed = 0 -- The ySpeed is reset
      player.inAir = false
   end
end
And that should be all. Here's the code with the modifications in full.

Code: Select all

function love.load()
   player = {
      x = 280,
      y = 380,
      ySpeed = 0, -- the Speed we'll add to the Y movement
      gravSecond = 1.5, -- gravity amount
      inAir = false
      }
end

function love.keypressed(key)
   if key == "up" and player.y < 400 and not player.inAir   then -- Are we on the ground?
      player.ySpeed = -20 -- Make us add a negative, to move up
      player.inAir = true
   end
end

function love.update(dt)
   frame = dt * 30 -- I do this just because I think better this way
   player.ySpeed = player.ySpeed + player.gravSecond * frame -- We add gravity to the ySpeed
   player.y = player.y + player.ySpeed * frame -- We add ySpeed to the player's y position
   if player.y > 400 then -- Are we on the ground?
      player.y = player.y - player.ySpeed * frame -- If we are, We undo that moving down
      player.ySpeed = 0 -- The ySpeed is reset
      player.inAir = false
   end
end



function love.draw()
      love.graphics.rectangle("fill", player.x, player.y, 40, 40) -- Draw it!
end
Hopefully that helps

Qcode

User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: How can I make my character jump?

Post by BEANSFTW » Mon Apr 16, 2012 7:55 pm

it works!
Thanks for the help

User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: How can I make my character jump?

Post by Refpeuk » Mon Apr 16, 2012 10:32 pm

So sorry for my rather short and rude reply. Moral: never browse forums in a bad mood.
It was the best of times, it was the worst of times . . .

User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: How can I make my character jump?

Post by Puzzlem00n » Mon Apr 16, 2012 11:11 pm

STOP THE PRESSES! I think I have a better fix for this.

Code: Select all

function love.load()
	player = {
		x = 280,
		y = 380,
		ySpeed = 0, -- the Speed we'll add to the Y movement
		gravSecond = 1.5 -- gravity amount
		}
end

function love.update(dt)
	frame = dt * 30 -- I do this just because I think better this way
	player.ySpeed = player.ySpeed + player.gravSecond * frame -- We add gravity to the ySpeed
	player.y = player.y + player.ySpeed * frame -- We add ySpeed to the player's y position
	if player.y > 400 then -- Are we on the ground?
		player.y = player.y - player.ySpeed * frame -- If we are, We undo that moving down
		player.ySpeed = 0 -- The ySpeed is reset
	end
	if love.keyboard.isDown("up") and player.ySpeed == 0 then -- Should we be jumping, and can we?
		--This works because the ySpeed is only zero when we aren't already jumping.
		player.ySpeed = -20 -- Make us add a negative, to move up
	end
end

function love.draw()
		love.graphics.rectangle("fill", player.x, player.y, 40, 40) -- Draw it!
end
That way you don't have to mess with booleans, which I prefer to avoid.
I LÖVE, therefore I am.

User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: How can I make my character jump?

Post by Puzzlem00n » Mon Apr 16, 2012 11:17 pm

P.S. If you don't get it, I'd be happy to make a longer, more tutorial-ish explanation, if you want.
I LÖVE, therefore I am.

Post Reply

Who is online

Users browsing this forum: Google [Bot], randomnovice and 4 guests