Jump to content
  • Advertisement
Sign in to follow this  
quiltkickkiller

Help with a timer problem?

This topic is 2589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, it's probably best if I explain what I'm trying to do that I can't exactly understand how to implement.

I'm trying to make a menu screen where a cursor can move up and down and select things.

I want to make the cursor, when either down or up is first pressed, to move one space up(the moving is not the issues it's the timing)or down ...

BUT I want it to move a space, stop for about one second, then keep moving as the key is held down. I DON'T want it to just move upon key pressing and holding because
the cursor will just fly up and down. Without a timer of some kind it can't move spaces and give a chance to remain in a space long enough or maneuver accurately and in a not so fast time.

Sort of like in Final Fantasy(if anyone here has played those games). So ... I don't really know how to implement a timer to accurately do this. I'm using SDL because that's the only graphics API I can actually use, install and understand so far(and with C++).

Everything else is fine, I just need, for the cursor in the menu screen, to move one space, wait about a second, then keep moving through the following spaces available in a faster speed as the key is held. Every time it's released and pressed again this should happen, the waiting, key press.

Should I make a function with timer controls/data and pass the data from other functions, make a global timer container, etc.?

I don't really know and I've never really worked with "timers" like this to understand how it works fully, how to implement it, what most mean by "timer," etc.

But I can handle everything else at this point as far as a menu screen goes. I just need help with the whole "timer" bit.

Share this post


Link to post
Share on other sites
Advertisement

BUT I want it to move a space, stop for about one second, then keep moving as the key is held down. I DON'T want it to just move upon key pressing and holding because
the cursor will just fly up and down. Without a timer of some kind it can't move spaces and give a chance to remain in a space long enough or maneuver accurately and in a not so fast time.




Take a look at this if you are confused about the basics with SDL timers. You probably want to encapsulate the functionality into a class for convenience purposes.

If you know how to use timers, all you need is to start a timer whenever you move the cursor. Where you move the cursor, put it inside an if-statement that only lets it move if enough time has elapsed. Here is an example:




Timer timer;
timer.Start();

//.....

if (timer.GetTime() >= 1000) /* 1000ms == 1sec */
{
cursor->Move();
timer.Restart();
}




Note that the timer variable is only declared once per instance (you don't make it every loop).

Share this post


Link to post
Share on other sites

You probably want to encapsulate the functionality into a class for convenience purposes.

[/quote]

That's okay. I don't use object-oriented programming. But thanks for the effort in trying.

I'll probably figure out the rest myself.


Share this post


Link to post
Share on other sites
Just store the last time the cursor moved, and if the current time minus the last time is greater than some threshold then move again.


struct cursor {
float x, y;
int last_tick;
};

cursor c;
cur_tick = get_current_time_in_ms();
if (key up pressed && key is repeated) { // can't remember how you check this in SDL, think there's some flag or something in the keyevent that tells you if it's a repeated key (key being held down)
if ((cur_tick - c.last_tick) >= 1000) { // if you hold down the up key, the cursor will take 1 second (1000 ms) to move to the next stop
move cursor up;
c.last_tick = cur_tick; // update the time it last moved
}
} else if (key up pressed && not repeated) {
// just move without checking time, but still update last_tick
move cursor up;
c.last_tick = cur_tick;
}

Share this post


Link to post
Share on other sites
Dude, oop is the key to managing projects larger than a few hundred lines of codes. If you don't use that, you're gonna end up with a mess of variables and functions everywhere. OOP is use to keep code cleaner and easier to understand and maintain. There's no drawback for using it, so arguing against it is just plain stupid. Imagine if you're house has no room, it would be pretty weird, having the oven right beside you're bed for example...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!