Decrease and increase character energy

Hi

In my game the idea is that if the character is moving, then his energy increases. On the contrary, as soon as he stops, the energy starts decreasing. 

I was tryin to do this with performWithDelay function, but with no succes. Here are the functions involved. Where you can see the dotted line, there was the timer.performWithDelay(1000, de(in)creaseEnergy) call. 

local function decreaseEnergy(arm) energyH[arm.myName] = energyH[arm.myName] - 1 leftValue.text = energyH.handL rightValue.text = energyH.handR end local function increaseEnergy(arm) energyH[arm.myName] = energyH[arm.myName] + 0.5 leftValue.text = energyH.handL rightValue.text = energyH.handR end local function energy(event, arm) --event = event.phase, arm = event.target if (arm.myName == "handL") or (arm.myName == "handR") then if (event == "ended") and (arm.bodyType == "static") then --energy down, ................ elseif (event == "began") or (arm.bodyType == "dynamic") then --energy up, ................ end end end

Then I include the function ENERGY into my startDrag function which is attached the to button listeners. Is there a better way to this? Thank’s for all the efforts…

Tomaz

Solved it, hope it helps other developers. Bellow is the code. I have created two timers, for each element one. Event then changes the status of the button.

local function energyLeft() if (button3.status == "active") then energyH.handL = energyH.handL - 1 leftValue.text = round(energyH.handL, 0) elseif (button3.status == "pasive") then energyH.handL = energyH.handL + 0.25 leftValue.text = round(energyH.handL, 0) end end local function energyRight() if (button4.status == "active") then energyH.handR = energyH.handR - 1 rightValue.text = round(energyH.handR, 0) elseif (button4.status == "pasive") then energyH.handR = energyH.handR + 0.25 rightValue.text = round(energyH.handR, 0) end end myTimerL = timer.performWithDelay(100,energyLeft,0) timer.pause(myTimerL) myTimerR = timer.performWithDelay(100,energyRight,0) timer.pause(myTimerR)

Solved it, hope it helps other developers. Bellow is the code. I have created two timers, for each element one. Event then changes the status of the button.

local function energyLeft() if (button3.status == "active") then energyH.handL = energyH.handL - 1 leftValue.text = round(energyH.handL, 0) elseif (button3.status == "pasive") then energyH.handL = energyH.handL + 0.25 leftValue.text = round(energyH.handL, 0) end end local function energyRight() if (button4.status == "active") then energyH.handR = energyH.handR - 1 rightValue.text = round(energyH.handR, 0) elseif (button4.status == "pasive") then energyH.handR = energyH.handR + 0.25 rightValue.text = round(energyH.handR, 0) end end myTimerL = timer.performWithDelay(100,energyLeft,0) timer.pause(myTimerL) myTimerR = timer.performWithDelay(100,energyRight,0) timer.pause(myTimerR)