Results 1 to 4 of 4
  1. #1
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default RPG Streaming Text With Timer class help

    Hey guys so I've been developing a 2d RPG game in java for some time now and I'm trying to get the NPC's to have a box of streaming text below the player where their message plays out when the player hits the space bar. I'm trying to achieve this by using the Timer class in java and using a delay. This is what I have so far:
    Java Code:
    public void streamTextG(String aText, final Graphics g)
            {
                i = 0;
                str = "";
                text = aText;  
                g.setColor(Color.WHITE);
                g.fillRect(Core.talk.x, Core.talk.y, Core.talk.width, Core.talk.height);
                
                
            messageStreamer = new Timer(50, new ActionListener() {
                
            @Override
            public void actionPerformed(ActionEvent ae) {
                    int len = text.length();
                    str = str + Character.toString(text.charAt(i));
                    i++;
                    g.setColor(Color.BLACK);
                    g.drawString("hello", 41, 532);
                    System.out.println(i);
                    System.out.println(text.length());
                    System.out.println(str);
                if(i == text.length())
                {
                   speechPrompt.isDone = true;
                   //i = 0;
                   str = "";
                   messageStreamer.stop();
                }else{
                   messageStreamer.restart();
                }
              }
    
          });
                
                messageStreamer.start();
            }
    My results are wacky and inconsistent because the i value doesn't increment up. but stays at 1 for some time and just acts randomly, sometimes finishing and sometimes not. Along with that, the Timer doesn't actually stop but continues over even if the value of i = the messages' length. Here is a snippet from my streaming classes constructor just to show you guys for context:
    Java Code:
    public class Utilities {
            private String text;
            private Graphics g;
            private int x = 0;
            private int i = 0;
            private int z = 0;
            private String str;
            private Timer messageStreamer;
    Thanks and I hope you guys can help me!

  2. #2
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: RPG Streaming Text With Timer class help

    Also it has drawString as "hello" just cause I'm also having problems with the display but it should be g.drawString(str.....) and same with the commented out i = 0

  3. #3
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: RPG Streaming Text With Timer class help

    anyone have any idea?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: RPG Streaming Text With Timer class help

    By streaming, I assume you mean displaying the text in bits rather than all at once?
    So (each line represents an iteration of the loop):
    Hello,
    Hello, Mr
    Hello, Mr Bond

    (etc etc).
    ?
    And this is in a JLabel?
    Actually, looking at it you're writing directly to the graphics.
    Can I suggest adding a JLabel at the bottom temporarily so you are just trying to change the text in it and not having to worry about the graphics side.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Am I using the timer class correctly?
    By Astralogic in forum New To Java
    Replies: 1
    Last Post: 09-16-2012, 12:41 PM
  2. Tooltip Text timer
    By rvd1ofakind in forum New To Java
    Replies: 5
    Last Post: 02-25-2012, 08:18 AM
  3. Timer Class Issue
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 10:31 PM
  4. Stop a timer in other class
    By warchieflll in forum Advanced Java
    Replies: 15
    Last Post: 02-02-2011, 08:13 PM
  5. Help with Timer Class
    By morfasto in forum New To Java
    Replies: 2
    Last Post: 11-03-2009, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •