how to kill time?

sorry for noob question (and more to come 'til I get the hang of this :wink:

I have this functions:

-- set initial timer speed  
local speedTimer = 6000  
-- update time  
if (speedTime \> 1000) then  
local function fnSpeedTime()  
 speedTimer = speedTimer - 100  
end  
-- run timer every 3 seconds until it speedTimer is 1000 (1 second)  
SpeedManager: timer.performWithDelay(3000, fnSpeedTime,0)  
else  
timer.cancel(SpeedManager)  
end  
  
-- do something  
local function fnDoSomething()  
 -- something  
end  
  
-- regular timer with 'variable' speedTimer controlling frequency)  
doTimer:timer.performWithDelay(speedTimer, fnDoSomething,0)  
  

The problem, is that even though the speedtime gets updated, the IF statement doesn’t seemed to go in effect and I’m going into negative values causing the ā€˜viewer’ to crash :frowning:

Any help will be greatly appreciated.
Thanks,
RD [import]uid: 7856 topic_id: 4092 reply_id: 304092[/import]

This is what I would do

[blockcode]
local speedTimer = 6000
local fnSpeedTime, fnDoSomething, speedManager, doTimer

function fnSpeedTime()
if speedTimer > 1000 then
speedTimer = speedTimer - 100
else
cancel.timer( speedManager )
end
end

function fnDoSomething()
Something
end

doTimer = timer.performWithDelay( speedTimer, fnDosomething, 0 )
speedManger = timer.performWithDelay( 3000, fnSpeedTime, 0 )

[/blockcode] [import]uid: 7911 topic_id: 4092 reply_id: 12680[/import]

Thanks jstrahan, I think that worked (except the cancel.timer, I switch to timer.cancel :wink:
However, this ( speedTimer, fnDosomething, 0 ) doesn’t seem to be working.

when I overwrite the ā€˜speedTimer’ to 1000, I can see ā€˜something’ happening every second.

Any ideas? [import]uid: 7856 topic_id: 4092 reply_id: 12687[/import]

Not sure but try this I’m on iPad right now and can not test

[blockcode]
local speedTimer = 6000
local fnSpeedTime, fnDoSomething, speedManager, doTimer
Ā 
function fnSpeedTime()
Ā  Ā  if speedTimer > 1000 then
timer.cancel(doTimer)
Ā  Ā  Ā  Ā  speedTimer = speedTimer - 100
doTimer = timer.performWithDelay( speedTimer, fnDoSomething, 0 )
Ā  Ā  else
Ā  Ā  Ā  Ā  timer.cancel( speedManager )
Ā  Ā  end
end
Ā 
function fnDoSomething()
Ā  Ā  Something
end
Ā 
doTimer = timer.performWithDelay( speedTimer, fnDoSomething, 0 )
speedManger = timer.performWithDelay( 3000, fnSpeedTime, 0 ) Ā 
[/blockcode]
[import]uid: 7911 topic_id: 4092 reply_id: 12690[/import]

Hey y’all…still struggling with this.
Jstrahan example doesn’t seemed to work.

To recap, I have updated my code to something move ā€˜real world’ and I just need to get the following working:

--\> start physics engine and set the gravity  
local physics = require ("physics")  
physics.start()  
physics.setScale( 60 )  
physics.setGravity(0,0)  
-- display a new ball every 3 seconds  
local timeManager = 3000  
local ballSpeed = 6000  
local fnRandomBall = function()  
 local rndBall = display.newImage( "ball\_orange.png" ) -- grab the ball from pool sample  
 rndBall.x = 40 + math.random( 240 ); rndBall.y = 10  
 physics.addBody( rndBall, { bounce=0.1, radius=14 } )  
 rndBall.myName = "orange"  
 rndBall.isHit = false  
 transition.to(rndBall, {time=ballSpeed, y=360})  
end  
-- run the above function on a timer  
tmrBall = timer.performWithDelay( timeManager, fnRandomBall, 0 )  
-- update timeManager variable  
function fnSpeedTime()  
 if timeManager \>= 1000 then  
 timeManager = timeManager - 100  
 ballSpeed = ballSpeed - 200  
 local t = display.newText( timeManager, 115, 105, "ArialRoundedMTBold", 21 )  
 t:setTextColor( 255, 255, 255 )  
  
 else  
 timer.cancel( tmrSpeed )  
 end  
end  
-- run speed function  
tmrSpeed = timer.performWithDelay( 1000, fnSpeedTime, 0 )  

If you run this on a stand alone main.lua, you’ll see that ā€˜TimeManager’ does update, but in ā€˜tmrBall’ still spawning at 3 seconds…noticed as well that ā€˜ballSpeed’ does seem to be updating accordingly.

I also tried to just change ā€˜tmrSpeed’ to 10 (just loop 10 times), but is the same issue, ā€˜TimeManager’ doesn’t seem to spawn balls any faster.

Any additional help would be greatly appreciated.

Thanks,
RD [import]uid: 7856 topic_id: 4092 reply_id: 14559[/import]

As far as I understand, you cannot change the frequency of a Timer once you set it initially. So even though you have the frequency tied to the timeManager variable, it will only use the initial value of timeManager - which is 3000.

You would run into the same issue with GameSalad. Something like: Timer Every random(3,10) seconds would NOT trigger the Timer like this: 3,5,7,8,4,10,6,3,etc… Instead, it would choose the initial random number, say 4, and trigger the Timer like this: 4,4,4,4,4,4,4,etc…

The same thing is happening here. Both Corona and GameSalad are built upon Lua, so maybe that is simply a limitation of the language.

It seems that you cannot have a Timer that fires repeatedly with a variable frequency.

The workaround is to wrap the Timer in a function, and then call the function repeatedly.

This code will give you want you want:

-- declare variables  
local myTimer  
local timeManager = 3000  
local ballSpeed = 6000  
local t = display.newText( timeManager, 115, 105, "ArialRoundedMTBold", 21 )  
local physics = require ("physics")  
  
--\> start physics engine and set the gravity  
physics.start()  
physics.setScale( 60 )  
physics.setGravity(0,0)  
  
-- set text color  
t:setTextColor( 255, 255, 255 )  
  
local function spawnRandomBall()  
  
 local rndBall = display.newImage( "ball\_orange.png" ) -- grab the ball from pool sample  
 rndBall.x = 40 + math.random( 240 )  
 rndBall.y = 10  
 physics.addBody( rndBall, { bounce=0.1, radius=14 } )  
 rndBall.myName = "orange"  
 rndBall.isHit = false  
 transition.to(rndBall, {time=ballSpeed, y=360})  
  
 if timeManager \>= 1000 then  
 timeManager = timeManager - 100  
 ballSpeed = ballSpeed - 200  
 t.text = timeManager  
 end  
  
 -- call this function again  
 myTimer = timer.performWithDelay( timeManager, spawnRandomBall)  
end  
  
-- set everything in motion  
spawnRandomBall()  

Just paste that into your main.lua file and you should be good to go.

Hope this helps!
Joe [import]uid: 8444 topic_id: 4092 reply_id: 14564[/import]

Awesome Joe (haven’t tested, have to wait 'til after work), but I’m confident you did and it does work :wink:

You mentioned it is the same issue in GS; however, I’m trying to recreate something I did in GS and is this exact same concept.

In GS, I have an actor with those same ā€˜global variables’ (ballspeed and timemanager) and inside the actor behavior, I have ā€˜move’ with speed set to ā€˜ballspeed’.

I then have another actor that controls how often they get spawned, based on ā€˜timemanager’.

But then again, this now makes sense cuz is wrapped around a function, which I guess could be the same as wrapped around a behavior?

Thanks again…you might have just given me the greatest xmas present ever!

Glad to have you around this side of the woods :wink:

RD [import]uid: 7856 topic_id: 4092 reply_id: 14591[/import]

There’s really no ā€œlimitsā€ when it comes to this stuff. If the built-in timer function doesn’t let you change the frequency at which it fires off, try using ā€œfaux timersā€ as I like to call them:

http://ideveloper.kodingen.com/2010/creating-a-time-counter-and-practical-usage/

A tad bit more work, but should easily be able to pull off what you’re trying to do (change the timer length at specific intervals).

Hope that helps!

Jonathan Beebe [import]uid: 7849 topic_id: 4092 reply_id: 14603[/import]

ok, this kind of works the way I want to…I just don’t have control on when ā€˜timeManager’ updates speed.

In GS, I had created an actor call ā€˜spawnBall’; set a behavior to spawn a ball every second (1sec).
My ā€˜ball’ actor has a move behavior to move down based on a global variable call ā€˜ballSpeed’ set on ā€˜speed’ box (starting at a value of 90 integer)
And last, I had an actor, ā€˜speedManager’ (don’t know why I call this one ā€˜timeManager’).
ā€˜speedManager’ will run every 30 seconds updating ā€˜ballSpeed’ until ā€˜ballSpeed’ was no more than 10.

What FMG provided, seems like is running every single second, therefore, ā€˜timeManager’ is updating its speed and everything is moving too fast, instead of every 30 seconds.

Does that make sense?

As I started writing this reply, I kind of saw the logic and I played with os.time like jonbeebe pointed out, but I think I’m running into the same issue, how do I update ā€˜timeManager’ or ā€˜ballSpeed’ on a timer with different speed to mimic my pseudo?

Again, any additional feedback would be appreciated.

so close for a final answer =\ [import]uid: 7856 topic_id: 4092 reply_id: 14629[/import]

shoot, I think I just figured it out:

  
-- declare variables  
local myTimer  
local mySpeed  
local timeManager = 1500  
local ballSpeed = 3000  
local t = display.newText( ballSpeed, 115, 105, "ArialRoundedMTBold", 21 )  
local physics = require ("physics")  
   
--\> start physics engine and set the gravity  
physics.start()  
physics.setScale( 60 )  
physics.setGravity(0,0)  
   
-- set text color  
t:setTextColor( 255, 255, 255 )  
--control speed  
local function fnSpeedManager()  
 if ballSpeed \>= 700 then  
 --timeManager = timeManager - 100  
 ballSpeed = ballSpeed - 300  
 t.text = ballSpeed  
 else  
 timer.cancel(mySpeed)  
 end  
end  
mySpeed = timer.performWithDelay( 10000, fnSpeedManager, 0 )  
--spawn random ball  
local function spawnRandomBall()  
 local rndBall = display.newImage( "ball\_orange.png" ) -- grab the ball from pool sample  
 rndBall.x = 40 + math.random( 240 )  
 rndBall.y = 10  
 physics.addBody( rndBall, { bounce=0.1, radius=14 } )  
 rndBall.myName = "orange"  
 rndBall.isHit = false  
 transition.to(rndBall, {time=ballSpeed, y=360})   
end  
   
-- set everything in motion  
myTimer = timer.performWithDelay( timeManager, spawnRandomBall, 0)  
  

so simple…just my logic sucked :stuck_out_tongue:
I’m not quite sure if this is optimal or best performance; could having multiple timers affect performance in the way I’m doing it here? I’m planning on having several.
Is it better to have a transition instead of just changing gravity to default 9.8?
I’ll probably won’t be around 'til next week, so thanks again to all for the help and merry xmas.

RD [import]uid: 7856 topic_id: 4092 reply_id: 14633[/import]

shoot, this ain’t working exactly how I want it.
I have it set up to run every second, but I actually need it to start at every 3 seconds, and then after 30 seconds update to run every 2.5 until it gets to .5.

any ideas?

Thanks,
RD [import]uid: 7856 topic_id: 4092 reply_id: 14691[/import]

Try this

Create a global variable called timeInHalfSeconds=0
and on called timeToExecute=6
then change your timer to go to the function every .5 seconds
Then inside function put if statement that say
if timeInHalfSeconds%timeToExecute==0 then
(what ever the function does)
timeInHalfSeconds = 0
else
timeInHalfSeconds=timeInHalfSeconds+1
end

then just change the timeToExecute when you need a lesser time to execute the function

Hope this helps I’m on the iPhone
Right now sorry i could be more specific but it’s tough typing all this from phone [import]uid: 7911 topic_id: 4092 reply_id: 14694[/import]

Or instead of every ,5 seconds make it every .1 second then change
timeToExecute=30 for 3 seconds then you could decrease by .1 seconds [import]uid: 7911 topic_id: 4092 reply_id: 14695[/import]

Thanks jstrahan,
I’ll try your suggestion and/or a combination of :slight_smile:

RD [import]uid: 7856 topic_id: 4092 reply_id: 14766[/import]

welcome let us know how it works out [import]uid: 7911 topic_id: 4092 reply_id: 14795[/import]