It seems that drawMultiLine does not accept String , but CharSequence . That way, you can probably implement your own CharSequence , which does not actually concatenate the two lines. Here's the implementation project:
public class ConcatenatedString implements CharSequence { final String left, right; final int leftLength; public ConcatenatedString(String left, String right) { this.left = left; this.right = right; this.leftLength = left.length(); } @Override public int length() { return leftLength+right.length(); } @Override public char charAt(int index) { return index < leftLength ? left.charAt(index) : right.charAt(index-leftLength); } @Override public CharSequence subSequence(int start, int end) { if(end <= leftLength) return left.substring(start, end); if(start >= leftLength) return right.substring(start-leftLength, end-leftLength); return toString().substring(start, end); } @Override public String toString() { return left.concat(right); } }
Use it as follows:
font.drawMultiLine(batch, new ConcatenatedString(SCORE_TEXT, score), 50f, 670f);
Internally, in your case, drawMultiLine only needs the length and charAt . Using ConcatenatedString , you create only one new object. Unlike when you use SCORE_TEXT + score , you create a temporary StringBuilder that creates an internal char[] array, copies the input characters, StringBuilder array if necessary, and then creates a final String object that creates a new char[] array and copies characters again. Thus, it is likely that ConcatenatedString will be faster.
source share