How to Program a dialog box with typewriter effect

Started by
18 comments, last by LAURENT* 8 years, 2 months ago

if I render the text it all types in one spot and lags the game


13. Thread.sleep(millisPerChar);

Is this called in your main thread? It will stop whatever thread it is called in. If this is called in another thread other than your main thread, I'd be concerned about how you are handling thread lifetime management.

A more common way to do this is for your main game loop to update a clock or timer. You might have something more like this:

void typeWriter(String message, long millisPerChar, ...) {
  ...
  // Record when we are starting the message, and how long it should run.
  m_startTime = Simulation.Clock.Now();
  if(millisPerChar > 0 ) {
    m_millisecondsPerChar = millisPerChar;
  } else {
    m_millisecondsPerChar = DEFAULT_MILLIS_PER_CHAR;
  }
  ...
}
 
void update()
{
  // Display up to the full length of the message, allowing for skipping
  int length = m_message.length;
  if(!SkipWasPressed) {
  length = Math.min( ((Simulation.Clock.Now()-m_startTime) / m_millisecondsPerChar), length);
  DrawString( m_message.substring(0,length), ...
...
Advertisement

So I took your advice and learned more about GUI's and then looked int substrings and all the good stuff like that biggrin.png The problem I'm having is if I render the text it all types in one spot and lags the game. Im probably doing this all wrong but id appreciate it of you could look at what I've done and send me in the right direction. http://pastebin.com/xM0g51bV

If you want to use this:

g.drawString(message.substring(i,i+1), x, y);

Specifically the (i, i+1) you'll need to update the x and y like frob suggests.

I don't know what the architecture of the rest of it is like, but I would be inclined to redraw the string every time I add another character.

*clear screen*

*increment character counter*

*draw string* a'la

g.drawString(message.substring(0,current_index), x, y);

That might just be me though. I also don't like Java a whole lot because it requires me frequently revise my expectations, and designs. In other words my advice might have no merit.

edit: read the other two posts.

Frob's code example is exactly how I'd try to go about it.


That might just be me though. I also don't like Java a whole lot because it requires me frequently revise my expectations, and designs. In other words my advice might have no merit.

ya I know java isn't the best but I'm on a mac and I want a game that runs on all systems.


Hmm, I wonder how that works at all

hahaha my thoughts too XD


A more common way to do this is for your main game loop to update a clock or timer. You might have something more like this

So would I put the code in my game loop? (Sorry for being a noob)


A more common way to do this is for your main game loop to update a clock or timer. You might have something more like this

So would I put the code in my game loop? (Sorry for being a noob)

Do you remember colouring as a child? Did you draw inside or outside the lines? Programming is a constant exercise in drawing outside the lines, until you learn better. Start with that.


Simulation.Clock.Now();

What does this bit mean I'm having trouble understanding it

It is pseudocode, not specifically Java. More or less accurate to the logic you can implement.

Typically games have a simulation of some sort, with an integer value used for the clock. This allows you to pause your game simulation, do effects like slow motion, or high speed, or whatever else you need. Generally this is a 32-bit or 64-bit integer starting at the first moment of the game. If you choose a simulator time measured in milliseconds, 32-bits gives roughly 24 simulation days of play, 64-bits in milliseconds is about 2 million centuries of simulated time. Choose one or the other depending on how long your game's simulation is likely to cover.

The two lines are to get your current clock time and to subtract it from the current clock time, giving the elapsed time so you can see how far along you need to draw.

So I was having trouble understanding your suggestions so i spent some time doing more research and trying out some new techniques. I found a nifty tutorial but it was for flash so i tried to convert the code int java. It isn't working but I feel that I am on the correct track. Please look over the code and help me find whats wrong http://pastebin.com/TyHtKaNC

(the reason for the IF statement with the done boolean is if I told writChar to remove itself it would cause a stack overflow error and that seemed to fix it).

I would create 4 rows and have a limit for how many characters can be in each row (Empty spaces count as characters). Then I would type in a sentence within my limit of all rows combined and have code print each letter til each row is finished.

This is nearly instantanous idea that flew toward my head fast. Logic was minimal.

This topic is closed to new replies.

Advertisement