Page 1 of 1

Implementing power-ups

Posted: Sat Apr 22, 2017 6:42 pm
by panther99
I want to have power up which increments player's speed stored in player.speed, but I want to make it last only for some time - I want to do it asynchronously with callback function which would be called after defined period. What's the easiest way to do this?

Code: Select all

function Player:speedUp()
  self.speed = self.speed + 1
  -- this should be postponed till the end of period
  setTimeout(period, function() {
    self.speed = self.speed - 1
  })
end

Re: Implementing power-ups

Posted: Sat Apr 22, 2017 7:28 pm
by raidho36
You can store the speed boost in a separate variable and simply add it to the baseline speed in the calculations. Then it would be easier and less error prone to handle it.

You will have to keep track of elapsed time yourself. It should be simple enough - love.timer.getTime function returns current time starting from some arbitrary point. When powerup is activated, query this value and record the offset. Then, by subtracting this offset from current time, you can tell how much time elapsed since then.

Re: Implementing power-ups

Posted: Sat Apr 22, 2017 7:41 pm
by panther99
Thanks, I completely forgot about love.timer.