Hi guys,
I was just doing some refactoring and came across my typewriter implementation I made a while ago. Wondering now what would be the best approach. I came up with 3 methods, which all achieve the same functionality. But I would like to know which would perform best. Would love to hear your opinions. The code is beneath.
local text1 = display.newText({ text = "", y = 80, font = native.systemFont, fontSize = 15 }) text1.anchorX = 0 local text2 = display.newText({ text = "", y = 160, font = native.systemFont, fontSize = 15 }) text2.anchorX = 0 local text3 = display.newText({ text = "", y = 240, font = native.systemFont, fontSize = 15 }) text3.anchorX = 0 local text = "abcdefghijklm nopqrstuvw xyz abcdefghijklm nopqrstuvw xyz" ------------------------------------------------------- -- Method 1 ------------------------------------------------------- for i=1, #text, 1 do timer.performWithDelay(i \* 100, function() text1.text = string.sub(text, 1, i) end) end ------------------------------------------------------- -- Method 2 ------------------------------------------------------- local lastTime = system.getTimer() local lastIndex = 0 Runtime:addEventListener("enterFrame", function(event) if event.time \>= (lastTime + 100) then lastTime = event.time lastIndex = lastIndex + 1 text2.text = string.sub(text, 1, lastIndex) end end) ------------------------------------------------------- -- Method 3 ------------------------------------------------------- local lastIndex2 = 0 timer.performWithDelay(100, function() lastIndex2 = lastIndex2 + 1 text3.text = string.sub(text, 1, lastIndex2) end, 0)