Implementing power-ups

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
panther99
Prole
Posts: 7
Joined: Sat Sep 03, 2016 7:21 pm
Location: Belgrade

Implementing power-ups

Post 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
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Implementing power-ups

Post 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.
User avatar
panther99
Prole
Posts: 7
Joined: Sat Sep 03, 2016 7:21 pm
Location: Belgrade

Re: Implementing power-ups

Post by panther99 »

Thanks, I completely forgot about love.timer.
Post Reply

Who is online

Users browsing this forum: No registered users and 83 guests