Game Development Reference
In-Depth Information
if (i+1 <wordsInCurrent) { currentText += " "; }
}
else {
newText += wordArray[i];
if( i+1 <wordArrayLength) { newText += " "; }
}
}
}
text.push(currentText);
maxLineWidth = this.measureText(currentText);
if(newText) {
subWidth = this.createMultilineText(newText,text);
if (subWidth>maxLineWidth) {
maxLineWidth = subWidth;
}
}
return maxLineWidth;
}
The preceding function,
createMultilineText
, performs two tasks:
•
Stores split lines of text in the
text
array
•
Returns the maximum width of the string with the
text
array
The function first replaces the newline (
\n
) characters with spaces and then splits the
text into words and stores them in the
wordArray
array.
The
while
loop iterates until the length of the
currentText
variable is not less
than the
maxWidth
variable. Basically, the code in the loop first concatenates the
wordsInCurrent
(which is 1 subtracted from the value of
wordArrayLength
) words
in the
currentText
variable and concatenates the rest of the words in the
newText
variable. It then checks the length of
currentText
, if it is still greater than the value
of
maxWidth
. Then, it copies
wordsInCurrent
(which is 2 subtracted from the value of
wordArrayLength
) to see if it fits. It continues to copy one word less to
currentText
until the width of the text is less than the value of
maxWidth
. Then, it invokes itself
recursively with the
newText
variable (concatenated words greater than the value of
maxWidth
), which holds the rest of the words after storing the
currentText
variable
in the
text
array. It also compares the length of the returned
subWidth
variable with
maxLineWidth
, to get the width of the longest string in the
text
array.