Detecting idle time in corona sdk - coronasdk

How to detect idle time in Corona? How to detect there is no user interaction or event with the device in a particular interval of time?The application what I am developing is based on user events.If the user doesn't provide any event for few minutes,the screen must be reloaded?Any idea about this with a sample code will be helpful.

Set up some counter which will count the idle time (maybe every second)...
When user touch the screen this timer will be set back to 0 - so when this counter reach some value (for example 60) then reload you page...
This is counter:
local idletime = 0
local function countidle()
idletime = idletime + 1
if idletime == 60 then
-- Code for restart
end
end
timer.performWithDelay(1000, countidle, 0)
Then do some function which will reset idletime value on touch...
Hope it helps ;)

You need a screen tap listener:
local function onScreenTap( event )
idletime =0
end
Runtime:addEventListener( "tap", onScreenTap )

Related

Why only does idle play from my animation script?

So I'm making a custom animation script for a custom character on Roblox. The AI works fine but it will almost always only play idle animations even with humanoid events.
What should normally happen is when the monster is Idle the idle animation should play. When walking the walk animation should play. And when the AI attacks, the attack animation should play.
I've dried commenting out the idle animation part, but then no animations play at all.
Here's the code:
local myHuman = script.Parent.Humanoid
local walkAnim = myHuman:LoadAnimation(script.Parent.Walk)
local idleAnim = myHuman:LoadAnimation(script.Parent.Idle)
local jumpAnim = myHuman:LoadAnimation(script.Parent.Jump)
myHuman.Running:Connect(function(speed)
if speed > 3 then
walkAnim:Play()
else
walkAnim:Stop()
idleAnim:Play()
end
end)
myHuman.Jumping:Connect(function()
jumpAnim:Play()
end)
myHuman.Died:Connect(function()
for i,v in pairs(myHuman:GetPlayingAnimationTracks()) do
v:Stop()
end
end)
Try adding a print instead of the animation and see what happens and tell me if it prints anything.
myHuman.Running:Connect(function(speed)
if speed > 3 then
print("walk")
else
print("start idle")
end
end)
Edit: https://devforum.roblox.com/t/getplayinganimationtracks-is-deprecated/1075650 I believe that you must do
myHuman.Animator:getPlayingAnimationTracks()

Running multiple timers?

I've got nodemcu and trying to achieve the following.
- Every 5 mins send data to a remote website.
- Every 1 second check sensors
So have 2 functions:
function checkSensors()
print("checking sensors")
-- do some stuff here
end
function sendData()
print("Sending Data")
-- do some stuff here
end
tmr.alarm(0, 1000, 1, function() checkSensors() end )
tmr.alarm(0, 300000, 1, function() sendData() end )
If I comment out the first tmr then every 5 mins secs the function sendData is called. Likewise commenting out the first tmr the function sendData is called every sec.
However with both in on sendData is called.
How can I use both timers, or is there another way I'm supposed to do this.
The first parameter to tmr.alarm is the ID of the timer. Because you use 0 for both calls, this simply reconfigures one timer twice. If you want multiple timers, you need to pass different IDs for each one.
There are 7 static timers (0-6), though these are going away. You can create dynamic timers with tmr.create. The returned ID can be passed to tmr.alarm.

Can I make Lua wait for an event to finish before executing again?

I am using events in Lua I subscribe with:
Runtime:addEventListener("balanceChanged", onBalanceChanged)
And raise them with
Runtime:dispatchEvent({ name = "balanceChanged" })
Now in onBalanceChanged, I want to do some animation on the screen that may take a few seconds to finish - but during this time the same event can be dispatched again.
How to I ensure that the onBalanceChanged function is only executed one at a time, aka if it's currently executing, wait for it to finish before carrying on?
ex:
local isAnimOver = false
function playAnimation()
if not isAnimover then
isAnimOver = true
-- here play the animation
--- Once the animation is over then set the flag to
isAnimOver = false
end
end

event.other is nil in onCollision after resuming the scene with storyboard library [CORONA sdk]

I'm having a strange error developing with Corona SDK relating to collision-detection and the storyboard lib.
Here is my onCollision listener:
local function onBottomBorderCollision( event )
if (event.other.isBall) then
local index = table.indexOf( ballArray, other )
table.remove( ballArray, index )
event.other:removeSelf( )
event.other = nil
updateLife(false)
updateScore(false)
end
end
This works fine at first launch, but after getting back to the menu screen (using storyboard.goToScene("menu")) and replaying the game, now this listener will trigger the following error every time one of my ball hits the bottom border:
attempt to index field "other"(a nil value)
I do create the proper listeners in scene:onEnterScene(scene) so it's not something to do with them, moreover this other listener never generate the error:
local function onPlayerCollision( event )
if(event.other.isBall) then
xVel, yVel = event.other:getLinearVelocity( )
event.other:setLinearVelocity( event.other.XLinearVelocity, yVel )
end
end
I am stuck right now... please help!
Actually, this type of error is caused mainly due to to some active function call/timers/transitions, which is not yet cancelled before changing the scene.
attempt to index field "other"(a nil value)
The above error mentions that any object/ it's property is being called/fetched, but it is not in the current occurrence or the scene. So, check whether you are cancelling your timers, transitions and runtime event listeners.
In your case, it may be due to the failure in cancellation of collision detection function onBottomBorderCollision in runtime. If you are calling it in enterFrame, then you have to cancel it before scene change. YOu can do it as below:
Runtime:removeEventListener("enterFrame",onBottomBorderCollision)
Update: You can't stop the physics engine while collision check is running. So do as follows:
function actualSceneChange()
physics.stop() -- stop physics here
-- call scene chage
director:changeScene("menuPageName") -- call your scene change method
end
function initiatingSceneChange()
physics.pause() -- pause physics here
-- stop unwanted event listeners
Runtime:removeEventListener("enterFrame",onBottomBorderCollision)
if(timer_1)then timer.cancel(timer_1) end -- stop all your timers like this.
if(trans_1)then transition.cancel(trans) end -- stop all your transitions like this.
timer.performWithDelay(1000,actualSceneChange,1)
end
I believe the issue is because storyboard saves some variables in memory when you switch scenes. The display objects gets destroyed but the reference remains. You are probably initializing your display objects in CreateScene function which gets called only once if you do not remove your scene.
Destroying the scene in the enter scene function of menu would probably fix the issue.
This is done by the Storyboard.removeScene function.
On the "EnterScene" Function of menu.lua, add in a line to remove the scene. eg. if your scene name is game:
storyboard.removeScene("game")
You can know more about difference between destroyScene and purgeScene here

Corona Lua Questions

I just got started with Corona yesterday and love what I have seen thus far. I have run into a few issues with which I would like some help
It looks like a Corona app is set up to shut down as soon as the Home button on the phone is clicked. I have come across hints that this can be chanaged from build.settings but nothing more specific. What needs to be specified there?#
I imagine there are pause and resume events associated with when the app gets back/fore grounded?
I'd much appreciate any help with this
For iOS devices, add these in build.setting.
iphone =
{
plist =
{
UIApplicationExitsOnSuspend = false
}
}
Android will automatically suspend when home button is pressed.
See here.
For application suspend/resume handling, you can try:
function onSystemEvent( event )
if ( event.type == "applicationSuspend" ) then
--do stuff
elseif ( event.type == "applicationResume" ) then
--do stuff
end
end
Runtime:addEventListener( "system", onSystemEvent )
See here.

Resources