Hi,
I am struggling because I ve come up against something I cant solve using simple comparasons etc, I need to use the math library.
Ok, all I need to do is award another life when the score reaches 5000, 10000, 15000 etc. I can do this easily enough when the enemies have the same score value.eg. each enemy has a score of 100.
I can then simply do :
if currrentScore == 5000 then addthelife() end
The problem is that my enemies have different score values and the chances of hitting 5000, 10000, 15000 etc are remote.
I know I have too keep track of the current score and query the difference between the last score and the new score, if its over 5000 the award a new life.
But Iam struggling to get my head round it, I have the idea but implementing it is proving difficult.
So if anybody has an example any pointers it would be awesome.
Like I said my score variable is currentScore and to add a life I use a function called addLives().
thank you
One way to add a new life every 5000 points is just keep track of the score and the “life counter.”
local currentScore = 0 local lifeCounter = 0 local function addToScore(num) currentScore = currentScore + num lifeCounter = lifeCounter + num if lifeCounter \> 5000 then lifeCounter = 0 addLives() end end
So every time you add to the score, also add to the lifeCounter variable. When that hits 5K, reset it to zero and add a new life.
Jay
Just a small fix to Whye’s answer:
if lifeCounter \>= 5000 then lifeCounter = lifeCounter - 5000 addLives() end
Yeah, that improves it. Thanks!
Jay
ooooh, thanks for this guys it helped me get my head around it, these little snippets I find invaluable I often file them away for future use.
One way to add a new life every 5000 points is just keep track of the score and the “life counter.”
local currentScore = 0 local lifeCounter = 0 local function addToScore(num) currentScore = currentScore + num lifeCounter = lifeCounter + num if lifeCounter \> 5000 then lifeCounter = 0 addLives() end end
So every time you add to the score, also add to the lifeCounter variable. When that hits 5K, reset it to zero and add a new life.
Jay
Just a small fix to Whye’s answer:
if lifeCounter \>= 5000 then lifeCounter = lifeCounter - 5000 addLives() end
Yeah, that improves it. Thanks!
Jay
ooooh, thanks for this guys it helped me get my head around it, these little snippets I find invaluable I often file them away for future use.