Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

increase a variable value based on score increment
Started by veracode Nov 04 2019 11:08 AM

4 replies to this topic

Best Answer veracode , 04 November 2019 - 01:30 PM

There's any number of ways to handle it, this is one:

 

 
local startingFallRate = 100
local fallRateIncreasePerInterval = 10
local increaseFallRateInterval = 3
local score = 0
 
 
local recalculateFallRate = function ()
 
 fallRate = startingFallRate + (math.round(score / increaseFallRateInterval)) * fallRateIncreasePerInterval
 
end
 
 
local changeScore = function (amount)
 
  score = score + amount
  recalculateFallRate()
 
end
 
changeScore(3)  -- call this whenever points are scored with the value to add
 

this worked well, i had to set the increaseFallRateInterval  to 6 that way every 3 points it increases the fallRate by the interval 100

 

unless i did something wrong  but it for sure worked out

 

 

Thank you

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

Hello Again! 

 

im running into a wall but i believe its something very simple. 

 

i have a variable fallRate

 

fallRate = 100

 

 

i want to increase this fallrate by say 10 for every three points scored 

 

i can do this by of course writing out tons of functions  similar to this 

local function increaseFallRate()
  if (score == 3) then
    fallRate = fallRate + 2000
  end

but im sure there is a much easier way to do this rather than writing out 30 functions.

 

i tried 

local function increaseFallRate()
  if (score = score + 3) then
    fallRate = fallRate + 2000
  end

but this produced no results at all 

 

i set the fallRate to 2000 just so there is a dramatic change and i could see it immediately within the simulator.  

 

fallRate if the variable i use to set the linear Velocity of spawned objects. 

 

thanks in advance 



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

There's any number of ways to handle it, this is one:

 

 
local startingFallRate = 100
local fallRateIncreasePerInterval = 10
local increaseFallRateInterval = 3
local score = 0
 
 
local recalculateFallRate = function ()
 
 fallRate = startingFallRate + (math.round(score / increaseFallRateInterval)) * fallRateIncreasePerInterval
 
end
 
 
local changeScore = function (amount)
 
  score = score + amount
  recalculateFallRate()
 
end
 
changeScore(3)  -- call this whenever points are scored with the value to add
 



[TOPIC: post.html]
#3

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

  Best Answer

There's any number of ways to handle it, this is one:

 

 
local startingFallRate = 100
local fallRateIncreasePerInterval = 10
local increaseFallRateInterval = 3
local score = 0
 
 
local recalculateFallRate = function ()
 
 fallRate = startingFallRate + (math.round(score / increaseFallRateInterval)) * fallRateIncreasePerInterval
 
end
 
 
local changeScore = function (amount)
 
  score = score + amount
  recalculateFallRate()
 
end
 
changeScore(3)  -- call this whenever points are scored with the value to add
 

this worked well, i had to set the increaseFallRateInterval  to 6 that way every 3 points it increases the fallRate by the interval 100

 

unless i did something wrong  but it for sure worked out

 

 

Thank you



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

Cool - I made a mistake in not declaring fallRate at the top - it's currently a global variable. And you should probably use math.floor instead of math.round, otherwise a score of 5 will equal two intervals. 



[TOPIC: post.html]
#5

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

changing it to math.floor made it work according to the actual score interval instead of half. 

 

now that i read the documentation on math.round and math.floor it makes more sense. 




[topic_controls]
[/topic_controls]