How to make an object jump only once. in the LÖVE framework?

Asked

Viewed 324 times

11

I’m trying to make a game using the framework LÖVE for Lua, where the player is a ball and has to overcome obstacles and enemies but I’m having a problem making the jumps.

function love.keypressed( key )
    if key == "w" then
        print("Teste")
        objects.ball.body:applyForce(0, -300)
    end
end

By using this function whenever I press w, is printed the "Test" but the ball does not move, another alternative I tried was the isDown() but if it remained primed the ball would stop the stars. Some alternative/solution?

  • It would be interesting to know the mass of the ball, to see if it resists the movement due to an unfavorable mass/force ratio. In time: my knowledge in Lua are few... It would not be the case to try objects.ball.body:applyForce(0, 300) (signal switching)?

  • First of all thank you very much for the issues in my question, I’m new around here. The problem was the mass when I applied a higher force (in this case -2000), the ball actually jumped. The signal has to be negative because as the ball goes up the value of y decreases.

2 answers

6

You should check if the ball is on the ground before applying force. This will prevent the ball from flying endlessly, as is happening.

For this, you can check if the y velocity of the ball is zero (i.e., the ball is on the ground), before applying the force. You should have better options (I don’t know much about LÖVE), but you should. Must be something like:

function love.keypressed( key )
    if key == "w" and objects.ball.y_velocity == 0 then
        print("Teste")
        objects.ball.body:applyForce(0, -300)
    end
end
  1. https://love2d.org/wiki/Tutorial:Platformer_Jumping

Note: This solution only solves for the object walking on flat platforms (with zero inclination). For an inclined plane and the object to slide the previous solution will cause bugs as the object will be on the ground but will be moving in y. In this case, the solution is to check collision with the ground instead of comparing the speed y to zero.

3


To check if the ball is in contact with the ground (or whatever) you need to define the callback functions for the object collisions

g = true
w = love.physics.newWorld(0, 10, true)
w:setCallbacks(beginContact, endContact, nil, nil) --[[Define o nome da funçao a ser 
executa quando contato é estabelecido entre dois objetos e quando o mesmo termina]]

--Definimos agora a funcao
function beginContact (a, b, ev) --Objeto(fixture) a e b e evento de colisão
  g = true
end

function endContact(a, b, ev) 
  g=false
end

--Modificamos o tratamento de teclas
function love.keypressed( key )
    if key == "w" and g then
        print("Teste")
        objects.ball.body:applyForce(0, -300)
    end
end

This way the force will only be applied when g=true (ball in contact with the ground), note that the objects that participated in the collision event were not verified, in case there are other objects it will be necessary to implement this verification.

This implementation can be done using the setUserData() and getUserData() methods of the table returned by love.physics.newFixture()

bola.fixture:setUserData("Bola")
chao.fixture:setUserData("Chao")
function beginContact (a, b, ev)
   if a:getUserData() == "Bola"and b:getUserData() == "Chao" or 
       a:getUserData() == "Chao" and b:getUserData() == "Bola" then
      g = true
   end
end

Example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.