Hi,

I'm trying to make a game, so when the NPC talks to the player, I want the text to type it self

So it would be like:

H
He
Hey
Hey t
Hey th
Hey the
Hey ther
Hey there

Instead of just
Hey there.

I've attempted to do this using a for loop, but I end up with a messy text.

public void render(Graphics g){
    g.drawImage(background , 0, 0, null);
    g.drawImage(oak, (Game.WIDTH / 2) - (oak.getWidth() / 2) , (Game.HEIGHT / 2) - (oak.getHeight() / 2) - 55, null);
    g.drawImage(messageText, 5, (Game.HEIGHT - messageText.getHeight()) - 25, null);

    //The above is just some other stuff.

    String[] oakSpeech = new String[10];
    oakSpeech[0] = "Hello there";

    for(int i = 0; i <= oakSpeech[0].length; i++){
        drawStringWithShadow(oakSpeech[0].substring(i));
    }
}

The for loop doesn't work, it creates a jumbled up text which seems really messy. Anyone have any idea how to do this?

Thanks,
Neil

It looks like you draw all the strings at the same position. You need to draw each string at increasing y coordinates so they appear one below another

Hey James,

Thanks for that, I didn't mean incrementing the y value to have different drawn strings to be more viewable. I mean like a text that typed itself.

Something like http://www.dump.com/bestkinetic/

Instead of text just being instantly written to a screen.

Cheers

OK
You'll need:

a Javax.swing.Timer that will run a small method at regular intervals. This is key component that controls the speed of your animation. See the API doc for details.
a variable that shows how many characters should be displayed (init to 1)
in you print method just print that number of characters
in your Timer method increment the character count and call repaint(); (this will trigger a call to your paint method)

Hey James,

This works, although the text is printed in a very jerky manner.

It goes like this

Hey t
Hey th
Hey ther
Hey there

I think the timer has to be faster, I am currently using
(0, 60); as the TimerTask and the Timer Interval. If I go below this, it is
too fast and doesn't show me the output.

The reason I need this is because is I am creating a game,

So the other NPCs and other objects will be speaking to the player,
Instead of the NPCs talking like

"Hey there"

it needs to feel typed out, so "H", "He", "Hey"..

I was thinking about using a thread, but I'm not sure if I have to go that far to get this done, and I'm not even sure if it will do what I want to accomplish.

I've solved this, in the render method I've slowed the thread down and used another class for the constant substringing of the text.

Thanks all! :)

This question has already been answered. Start a new discussion instead.