integer variable to Numbers sprite sheet?

Hello everybody, I am trying to convert an integer variable to image with the help of a sprite sheet.
 

Basically I have a variable named achievement.

So lets say achievement = 3000

or achievement = 98

I want to be able to show the image for the number from the sprite sheet

so the number 3000 will show the image 3 the image 0 the image 0 and the image 0

so the number 98 will show the image 9 and the image 8

Hi!

That’s not too hard. Wait, let me go and find my code to copy.

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:

  1. Determine the maximum amount of digits your number can have. In my code it is 7 digits.

  2. Convert your number into a string with 7 digits using the format code.

  3. 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.

Hi!

That’s not too hard. Wait, let me go and find my code to copy.

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:

  1. Determine the maximum amount of digits your number can have. In my code it is 7 digits.

  2. Convert your number into a string with 7 digits using the format code.

  3. 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.