(Java) Printing text letter by letter in console- ft. Lag -


so i'm making game uses nothing console in java ide, have problem using delays. goal create method when taken text passed though parameters, prints letter letter delay of 50. (kind of text in pokemon series, how scrolls though each letter).

the problem have lag. i'm running amd fx-8350 cpu (4ghz, 4 cores+4 virtual cores). whenever change delay amount around , under 100 milliseconds, console prints half word, little bit of other half, half sentence, maybe character, etc.

i've tried code here (it works, lag):

public void scroll(string text){     int delay = 50;     actionlistener action = new actionlistener()     {         int counter = text.length();         @override         public void actionperformed(actionevent event)         {             if(counter == 0)             {                 timer.stop();             }             else             {                 system.out.print(text.substring(text.length() - counter, ((text.length() - counter) + 1)));                 counter--;             }         }     };     timer = new timer(delay, action);     timer.start();  }//end scroll 

and code (also works, same amount of lag):

public void scroll(string text){     for(int = 0; <= text.length(); i++){        try {                thread.sleep(1000);             }catch(interruptedexception ex){                thread.currentthread().interrupt();             }        system.out.print(text.substring(i, + 1));     } }//end scroll 

in conclusion, i'm guessing computer doing more "delaying" when try delay thread. idea's?

-thanks, eric

firstly, random notes code. wrong:

for(int = 0; <= text.length(); i++){ 

whether iterating through string, array, list, or whatever, usual pattern always

i = 0; < length; i++ 

i.e. using <= getting exception. unneeded:

text.substring(i, + 1) 

and looks worse in first example. can use text.charar(i).


i tried code alternative:

for(int = 0; < text.length(); i++) {     long start = system.currenttimemillis();     while (system.currenttimemillis() - start < 50) {      }     system.out.print(text.charat(i)); } 

it still didn't work in intellij. , code work fine in terminal, think intellij doesn't printing immediately. flush makes no difference documentation in outputstream hints @ might happening:

if intended destination of stream abstraction provided underlying operating system, example file, flushing stream guarantees bytes written stream passed operating system writing; not guarantee written physical device such disk drive.

so again, think ide problem , it's out of control of code.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -