# Displaying Text on The screen for a certain amount of time

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

## Recommended Posts

So Ive been at this for weeks and for what ever reason I cannot figure this out.

What I need to do is when the player hits an action key the game updates a boolean so a String is constantly displayed on the screen.

Then, I need a timer to activate which, after a certain amount of time, updates a boolean that allows the player to hit the action key and proceed to the next String.

I have found no tutorials that have helped me on this subject. I need to figure this out so I can proceed with my game. Please help.

##### Share on other sites
Why do you need those two booleans? It seems you only need the time of the last event. If enough time has elapsed, you let the player advance.

As for not finding a tutorial, you'll find logic like this tends to not have tutorials. They are details the programmer needs to reason out on their own.

##### Share on other sites

Why do you need those two booleans?

my issue is constantly displaying the text. it only shows when I hold the action key. It doesn't stick. I was thinking that by setting a boolean to true I could tell java that

while(boolean = true){

g.drawString(...)

}

##### Share on other sites

You could do something like this:

while (time < timeWhenTextShouldDissapear){
g.drawString(...)
}


Just update timeWhenTextShouldDissapear whenever you need the text to appear.

##### Share on other sites

You could implement a little state machine type thing. Your problem sounds like it has several discrete states, the initial waiting state, the showing string state, the state a user can now press the action button. You could neatly wrap that up in a simple object.

Your object might have a simple enum with a few states, a variable with what state it is in and a few methods such as OnAction(). The action method might look something like:

void OnAction()
{
switch(state)
{
case Idle:
state = ShowingString;
break;
case ShowingString:
// Some logic to check the time has passed
if(timePassed)
state = next state;
break;
// etc
}
}



In some of your other methods they will look similar, maybe you have an Update method that is doing your timings, that would behave differently depending on the state. Perhaps you have methods that can be called to check if a string should be displayed and what that string is. State machines are great, they are a little bit more effort than some of the other suggestions but they expand really well. I think as soon as you start to need several isolated variables to get something done it is a good candidate to making into a separate object.

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 16
• 11
• 9
• ### Forum Statistics

• Total Topics
632594
• Total Posts
3007309

×