Right now I'm programming a game that has an animated typing effect typically seen in older 1980's and 90's role-playing games. An example can be seen here.
The typing effect is generated by taking a target string and rendering a substring of it each frame. The length of the substring is initially zero, but it is incremented every few hundred milliseconds. This gives the impression that the string is being typed out. The pseudocode would more or less look like:
var message = "This is some dialogue.";
var substringLength = 0;
if (someTimeHasPassed)
{
render(message.substring(0, substringLength);
substringLength++;
}
This works great, but I'm using a garbage-collected language with immutable strings. Every few milliseconds, I create a new substring on the heap. Garbage collections become very frequent and I receive memory warnings on mobile devices.
Is there a way to generate this typing effect without creating lots of strings in memory?
render()
? What does yoursomeTimeHasPassed
check actually look like? \$\endgroup\$render()
function, as Phaser uses a scene-graph setup. The string is being updated with a loop with JavaScript'sString.splice()
function. During this time, the device becomes really warm and receives memory warnings from the OS. \$\endgroup\$