Okay, this is just a copy/paste from my project so it won’t be ready for use, but it does show the basic idea:
-
Determine the maximum amount of digits your number can have. In my code it is 7 digits.
-
Convert your number into a string with 7 digits using the format code.
-
Go through each of the 7 digits from left to right. If the number is a zero, set the alpha value to zero. If it is not, set this and the following digits to alpha 1. Check what the character code is for each of the next digits and use this so set the right frame in your sprite.
And here’s the code you can base your code off:
local score7digits = string.format("%07d", UI.scoreCounterCurrent) local leadingZeros == true for i = 1, 7 do local digitCode = string.byte(score7digits, i)-48 if digitCode ~= 0 then leadingZeros = false end scoreCounterGroup[i]:setFrame(UI.sheetInfo:getFrameIndex("Number-"..digitCode)) if leadingZeros == true then scoreCounterGroup[i].alpha = 0 end end
scoreCounterGroup is a displayGroup you should create in advance, that has 7 sprites spaced out at an equal distance on the X-axis.