Jumping

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
Love2dNewbie
Prole
Posts: 7
Joined: Fri Dec 13, 2013 5:11 pm

Jumping

Post by Love2dNewbie »

I've recently started programmng my very first game, I got my player jumping and walking, ...
But when the player jumps, he jumps upwards and he falls straight through the ground.
I'm using a rectangle I drew using love.graphics for the ground.
I want the player to stop at Y = 276.
Code:

Code: Select all

function love.load()
  player = {} 
  player.x = 10 
  player.y = 276
  player.velx = 80
  player.vely = 0
  player.grav = 400
  player.jumpheight = 300
  player.img = love.graphics.newImage("textures/char.png")
  enemy = {}
  enemy.x = 300
  enemy.y = 287
  enemy.vel = 30
  enemy.img = love.graphics.newImage("textures/enemy.png")
  print('Game Loaded')
  print('v 0.1')
  love.graphics.setBackgroundColor(0, 255, 255) 
end


function love.draw()
   love.graphics.setColor(103, 164, 21, 255)
   love.graphics.rectangle("fill", 0, 300, 800, 300) 
   love.graphics.setColor(240, 255, 255, 255)
   love.graphics.draw(player.img, player.x, player.y, 0, 1)
   love.graphics.draw(enemy.img, enemy.x, enemy.y, 0, 1)
end

function love.update(dt)
  if player.vely ~= 0 then 
        player.y = player.y - player.vely * dt 
        player.vely = player.vely - player.grav * dt
        if player.y < 0 then 
            player.vely = 0
            player.y = 276
        end
    end
 if love.keyboard.isDown("right") then
      player.x = player.x + player.velx * dt
 end
 
 if love.keyboard.isDown("left") then
   player.x = player.x - player.velx * dt
 end
 
 
 if love.keyboard.isDown("escape") then
     love.event.quit()
 end  
end



function love.keypressed(key, unicode)
  if key == " " then
        if player.vely == 0 then 
            player.vely = player.jumpheight
      end
    end
end

function love.keyreleased(key, unicode)
  
end

function love.quit()
  print("exiting..")
end
Help is appriciated. :awesome:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Jumping

Post by Robin »

Don't you mean, instead of if player.y < 0 then: if player.y > 276 then?
Help us help you: attach a .love.
User avatar
Love2dNewbie
Prole
Posts: 7
Joined: Fri Dec 13, 2013 5:11 pm

Re: Jumping

Post by Love2dNewbie »

:D this helped
I took that off the wiki and tweaked it a little bit.
Post Reply

Who is online

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