Как нарисовать строку на экране символ за символом - PullRequest
1 голос
/ 21 марта 2019

У меня есть код ниже, который создает задержку между рисованием символов из строки, это работает с использованием println (), однако не работает при использовании функции text (). Код должен ждать выделенное время, а затем печатать следующий символ, я действительно не уверен, что делаю неправильно.

int startTimer;
int waitTime = 500;
boolean funcRun = true;

void setup(){
 size(500, 500);
 startTimer = millis();
}

void draw(){
  while(funcRun){
    textAnim("hello");
  }
}

void textAnim(String textInput){
int counter = 0;
int x = 10;

while(counter < textInput.length()){
    if(millis() - startTimer>waitTime){
      text(textInput.charAt(counter), x , 100);
       startTimer = millis();
       ++counter;
       x = x + 10;
    }
    funcRun = false;
  }
}

1 Ответ

1 голос
/ 21 марта 2019

Отображаемый экран обновляется в конце функции draw().Таким образом, ваш цикл while полностью выполнен и отображается завершенный текст.Вам нужно будет изменить код так, чтобы он постоянно обновлял / перерисовывал экран и обновлял отображаемый текст на основе временной петли.

Например, например:

int currentTime;
int waitTime = 500;
int characters_to_display = 0;
boolean stringComplete = false;

String textInput = "Hello"; 

void setup() {
  size(500, 500);
  currentTime = millis();
}

void draw() {
  // Update text to be shown. increaseCharIndex() is called while the text is not yet fully displayed
  if (stringComplete == false) {
    increaseCharIndex();
  }

  //Draw screen:

  // draw background to clear screen
  background(0);
  // display (a substring of) the text
  text(textInput.substring(0, characters_to_display), 10, 100);
}

void increaseCharIndex() {
  // if the waitperiod has passed, increase the number of characters to be displayed
  if (millis() - currentTime>waitTime) {
    currentTime = millis();
    characters_to_display++;
  }    
  // if the full text will be shown, end the call to increaseCharIndex()
  if (characters_to_display >= textInput.length())
  {
    stringComplete = true;
  }
}

enter image description here

...