Page 1 of 1

Jumping

Posted: Sun Dec 15, 2013 7:58 am
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:

Re: Jumping

Posted: Sun Dec 15, 2013 8:58 am
by Robin
Don't you mean, instead of if player.y < 0 then: if player.y > 276 then?

Re: Jumping

Posted: Sun Dec 15, 2013 9:13 am
by Love2dNewbie
:D this helped
I took that off the wiki and tweaked it a little bit.