0
\$\begingroup\$

So I'm making a game in Lua/Love2D and have a problem with my game logic. Mainly I have a spaceship that can use boost and stuff. That part is fine, I click space and it boosts up to speed quickly.

The problem is that if the person holds down space, they stay boosted, which is not the intended behavior. How do I make sure that once boost runs out, it decelerates to normal speed? This is how it is currently:

Note: Normal speed, 1000. Max boost possible speed, 3000.

if love.keyboard.isDown('space') then
    if player.v < 3000 and UICounters.currentboost > 0 then
        player.v = player.v + 200
        if player.v > 3000 then
            player.v = 3000
        end
        UICounters.currentboost = UICounters.currentboost - 1
    end
else
    if player.v > 1000 then
        player.v = player.v - 100
    end
    if UICounters.currentboost < UICounters.maxboost then
        UICounters.currentboost = UICounters.currentboost + dt
    end
end
\$\endgroup\$
2
\$\begingroup\$

While the player holds down the space key, the else part of your code is never reached, so the deceleration is not applied. Basically, you'd have to execute this if UICounters.currentboost is <= 0, even if space is being held down. You could achieve that by duplicating parts of your code, or you could change your initial check to something like this:

if love.keyboard.isDown('space') and player.v < 3000 and UICounters.currentboost > 0 then
    player.v = player.v + 200
    if player.v > 3000 then
        player.v = 3000
    end
    UICounters.currentboost = UICounters.currentboost - 1
else
    if player.v > 1000 then
        player.v = player.v - 100
    end
    if UICounters.currentboost < UICounters.maxboost then
        UICounters.currentboost = UICounters.currentboost + dt
    end
end

Now, your booster only fires if the space key is pressed AND the velocity is below the max AND the player has boost left. Otherwise it falls into the else part and the player decelerates. This might lead to some back and forth between 3000 and 2900 in certain situations, but I think this should be a good starting point for you to further finetune the behaviour.

\$\endgroup\$
  • \$\begingroup\$ This works perfectly! Thank you so much. :) Can't believe I didn't think of making the initial if statement. \$\endgroup\$ – Rivasa Feb 21 '17 at 13:44
-1
\$\begingroup\$

I think it is because you are using the keyboard.isDown which basically means if the Space bar is "held down" it will keep adding 200 to player.v untill you release it.

I suggest that you instead use:

function love.keypressed( key )
   if key == "return" then
      text = "RETURN has been pressed!"
   end
end

Read more about it here: https://love2d.org/wiki/Tutorial:Using_Input

\$\endgroup\$
  • \$\begingroup\$ Doesn't work when I use keyreleased. keypressed isn't what I need. I need them to be able to hold it down though. Unless I can put the keyisdown inside it... \$\endgroup\$ – Rivasa Feb 20 '17 at 18:15
  • \$\begingroup\$ Keyisdown inside it doesn't work either \$\endgroup\$ – Rivasa Feb 20 '17 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.