Help With Timers

Hi

I want to check whether a condition is ‘true’ and then check whether that condition is STILL ‘true’ 3 seconds later.

For example, let’s say a box is placed in the correct position on the screen. I then want a timer to start, run for three seconds and then check that the box is still in that correct position.

I have a Runtime event listener to check the correct position of the box, but what I’m struggling with is the second part of the code - getting a timer to run and then check the position again. If the box is moved from the correct position within that 3 seconds, then I want the timer to reset…

Please help before I end up pulling my eyeballs out…

Thanks [import]uid: 74503 topic_id: 14364 reply_id: 314364[/import]

have you tried the timer.performWithDelay function?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14364 reply_id: 53075[/import]

Thanks for the reply.

I have throught about that but I’ve not been successful in implementing it.

Here’s a code example of what I mean:

[lua]-- Create box

local myBox = display.newRect ( 0, 0, 50, 50 )
– Location Correct? True/False

local locationCheckFirst = false

local locationCheckSecond = false
– Simulate move the box to correct location

transition.to( myBox, { time = 2000, x = 100, y = 100})
– Check the location every frame - First check

local function firstCheck()

if myBox.x == 100 and myBox.y == 100 then

locationCheckFirst = true

end

end

Runtime:addEventListener( “enterFrame”, firstCheck )

– How do I wait 3 seconds and then check the location is still correct?
– I’ve included a bool (locationCheckSecond) for the second check[/lua]
I’d really appreciate any help with it… [import]uid: 74503 topic_id: 14364 reply_id: 53087[/import]

I can see what you are trying to do, but my brain is asking me why are you doing it this way?

What exactly are you trying to achieve? If you want to simulate a box moving through three positions and at each position that is reaches, you want to set a flag or a tickmark or something, then I would do it like this.

I would split the transition into two parts (we need to check for three positions, right) The start position to the first check point, when the transition completes, I can do whatever I want and then I start the second transition from that point to the next point and when that completes I can again do what I want. That way I would have full control over it. This would be similar to what RPG games do by moving a character to a particular location on the screen and then displaying the text for their speech., then move them again to another location, etc.

However if you wanted this for a game, where the User’s interaction would move the box and you need to check the position, that’s a different method then.

hope this helps you,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14364 reply_id: 53090[/import]

Sorry, maybe I wasn’t too clear on what I was trying to achieve.

This, isn’t my proper code for the game, it’s just a simplified example to demonstrate what I mean.

The movement in the code above (transition.to) is just to simulate a player moving the box to the correct position with their finger. The actual game will be physics based and the player has to manipulate on screen objects (in this simple example a single box) and line the correct blocks up with the correct location.

As you can see from the sample code, I can check that the box (only one in the simple example) so that’s not the issue. What I want to do though is then check that the box is still aligned correctly after 3 second has passed. This is to check that the box has not moved since it was first aligned as it will be balancing in the actual game.

If the level completed after it checked for the current position the first time (locationCheckFirst), then the balancing aspect wouldn’t be implemented and it would be too easy to complete that level. So, it has to be balanced for 3 seconds to complete the level.

Does that help?

Thanks again
[import]uid: 74503 topic_id: 14364 reply_id: 53094[/import]

you will have to be more specific, coz when it is in a game, you will use be looking at the move phase to position the box, and you can check for the position or a delta of the position if you want to snap into place if it is in a certain threshold.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14364 reply_id: 53100[/import]

In that above example though, once myBox has moved to the correct position, the runtime event listener and firstCheck function set the bool locationCheckFirst to true to indicate that myBox is in the correct position.

Now, I know physics is not active in that example, but let’s imagine that the user has in fact balanced it there on an imaginary pole. It needs to balance there successfully for 3 seconds.

Using that code above, just say that we wanted to check after 3 seconds that myBox is still in that exact same position (x = 100, y =100) (we know it will be as there’s no phsyics). How would I code it so that once locationCheckFirst = true, a timer starts and then after 3 seconds, if myBox is still in the exact same position, it sets locationCheckSecond to true?

From there, I can call a levelCompleted function etc…

[import]uid: 74503 topic_id: 14364 reply_id: 53104[/import]

As jayantv is now offline, maybe someone else can throw some light on this for me whilst he gets back…

please… [import]uid: 74503 topic_id: 14364 reply_id: 53115[/import]

Hi

You could try replacing the runtime enter frame by:

timer.performWithDelay (3000, firstCheck,-1)

This would do a check every 3 seconds(3000 milliseconds) and do it forever because of the -1 at the end of the parameter list.

Cheers

Mo [import]uid: 49236 topic_id: 14364 reply_id: 53121[/import]

@INSERT.CODE,
see everytime you let a little more of what you are trying to do. If you do not outline what you are trying to do upfront at first, you would have ended up wasting not only your own time but also others time in trying to provide you solutions that you might not have required in the first place.

About your question in light of your game where you need to check if the solutions was OK or not for a period of 3 seconds.

there are a couple of things you need to do

  1. On move phase you set the moving flag to true
  2. on ended phase of the move, you set the moving flag to false and start checking to true
  3. in the enterframe you start to check from the time that the checking flag has turned to true

[lua]local moving, checking, timerLeft

function call_funcation_Hoorah()
print(“You have completed this level”)
end

function onEnterFrame(event)
if moving then return end
if checking == true then
if timerLeft == 0 then
call_function_Hoorah()
else
timerLeft = timerLeft - 1
end
end
end[/lua]
in the ended phase write the code
[lua]moving = false
timerLeft = 180
checking = true[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14364 reply_id: 53135[/import]

@jayantv

I have not been trying to be cryptic or obscure what I’m trying to do in my questions, I was honestly trying to accurately and concisely describe what I was trying to achieve. The sample code was very simplified to merely demonstrate my questions further, without having to post several hundred lines of code. Quite honestly, being a learner/beginner with Corona/Lua, my original code would probably have made it harder for any helpers (like yourself) to assist me - it probably wouldn’t be the most elegant coding ever seen :slight_smile:

Anyway, a more important point is that I’d very much like to thank you for your help. Using the code you posted in your last message, and tweaking it to fit my original code, that solution works perfectly. You obviously know what you’re doing so thank you.

[import]uid: 74503 topic_id: 14364 reply_id: 53168[/import]

Glad it resolved your issue, good luck with your App.

if you outline what you are trying to achieve slightly better in words, it would be easier the first time around and then add a little code on where you feel the issue lies.

and honestly, if you had posted a whole lot of code, I would not have looked at it. I do not run the code posted either, so it is all about trying to first figure out what the person is trying to do and what the code is doing, then finding the best optimum solution on what should be done.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14364 reply_id: 53169[/import]