Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Can't make sense of timer function.

Author
Message
Nights Bane
14
Years of Service
User Offline
Joined: 15th Dec 2009
Location:
Posted: 27th Dec 2009 01:57
I've been trying to get my object to be animated for only 6 seconds. Can't get my head round it. Any suggestions?

Here's my code for those that are interested/ having the same problem. It uses functions for the mouse being pressed, and the animation of the object.

Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 27th Dec 2009 07:52
I can't actually test anything from this computer, but from looking at the code, I'd say the problem is right here, at this conditional:


First of all, begunAnimatedTime will never equal begunAnimatedTime+6000, just like 5 will never equal 5+2. I'm sure you meant to write something like "if (TimeNow <> begunAnimatedTime+6000)". Of course, this means that you can't keep changing the value of begunAnimatedTime to TimeNow like you do in the next line, or else you're back to what we had before. That conditional block should probably look something like this:



Notice that I use "<" as opposed to "<>". That's because you can never be sure, and shouldn't assume that the computer will be executing that conditional at any specific millisecond, like begunAnimatedTime+6000. Instead, we should check to see if we're still under that range, so that when TimeNow is greater than begunAnimatedTime+6000, the conditional won't evaluate to true anymore, and whatever we're animating will stop.

I hope this helps.
Nights Bane
14
Years of Service
User Offline
Joined: 15th Dec 2009
Location:
Posted: 27th Dec 2009 19:08
Your post has been helpful Kira, I've never played with time functions before, and cant seem to get my head round them. I tried this(as per your suggestions):



You've definately helped pointing out why my experiments have always failed thus far, because I was using a DO NOT EQUALS sign, when a limit was needed. So definately thank you for that. I've a feeling that maybe the reason this is not working now, is because I reset the value? I'm not sure.

Basically my animation is looping constantly now upon an arrowkey being pressed.
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 28th Dec 2009 05:47 Edited at: 28th Dec 2009 05:50
Well, the timer() function just returns the time the computer has been on in milliseconds. Using that information, we know that in order to cause an event to last a certain amount of time, we need to know when the event started, and we need to know when the current time - the event's start time >= the event length.

In your code, the main problem is that the time limit is only checked while the player is holding the walk key, and whenever the walk key is pressed, the starting time is reset. Also, there's no place where you stop the object from animating, so once you call your ANIMATE() function, the object will loop forever.

Hm, the solution I'd give depends on how your game works. Will the player walk only as long as the user holds down the walk key? Or will the player walk for a certain amount of time after the user taps the walk key? Judging by the timer limit we're trying to setup here, I'm guessing the latter?

Try something like this:


I can't test it here, but that should work in theory.

p.s. The way you wrote those conditionals makes me think you're a C/C++ programmer, yes?
Nights Bane
14
Years of Service
User Offline
Joined: 15th Dec 2009
Location:
Posted: 29th Dec 2009 00:50
Thanks for the swift input, I have been learning C++ recently but consider myself a java programmer- but the instant gratification that DarkBASIC offers is very satisfying. You could almost liken it to Lady Gaga or whatever pop music song is currently stuck in your head (In that its a guilty pleasure~!)

I can tell just from looking at your code it will probably be the solution to my quandry. Fresh eyes are always good when coding I reckon. Anyway, the missus is wanting on the PC, so thanks again for the help.
Nights Bane
14
Years of Service
User Offline
Joined: 15th Dec 2009
Location:
Posted: 30th Dec 2009 22:04
I didnt like the creation of that 'playerWalking' variable.

I've been trying it without, just got home, tried it and found it didnt work, heres my code, It feels as if its getting therenow though!

Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 30th Dec 2009 22:59
How about:
if (timer() - timeNow => 2000)
Meaning that 2 seconds have passed?

"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 31st Dec 2009 00:23 Edited at: 31st Dec 2009 00:25
@Nights Bane & Dream And Death:
The creation of a TimeNow variable is to reduce the calls to timer(), so every part of the main loop uses the same time as the current time. In theory, TimeNow and timer() will always have the same value. Thus, timer()-TimeNow will always equal 0 (or some very low number in actual). Dream And Death's suggestion would work as a method to check the time limit only if TimeNow were changed to begunAnimatedTime.

@Nights Bane:
Your code checks to see if the player has been walking for longer than the time limit only while the user is pressing one of the walk keys. To me, it seems that this is a problem, and once the user presses and releases a key, the player will never stop walking, because the time limit isn't checked. I used a PlayerWalking variable to register a keypress only once, and not every loop. I also placed the time limit checking outside of the keypress checking so that it can run independently.
Nights Bane
14
Years of Service
User Offline
Joined: 15th Dec 2009
Location:
Posted: 2nd Jan 2010 00:06
I came to a unique solution to my quandry, and placed 'stop object XXX' at the beginning of my loop, thus ending the need to use a timer unnecessarily. for those that wish to copy the method. heres some code ! : enjoy:

Login to post a reply

Server time is: 2024-09-28 14:23:16
Your offset time is: 2024-09-28 14:23:16