I was going to post this to AppGameKit snippets, but it's locked due to inactivity... If a mod could unlock it and wants to move this post to it, that'd be swell.
Anyway, I needed an old-fashioned analog counter effect for my game and it took way longer than I thought it would to figure it out, so I'm hoping to alleviate that issue for anyone else.
Below is code framework for a decreasing counter and an increasing counter. The main area needs a little adjusting to switch between increase and decrease. Should just be the function name and when to rotate the next digit (when the previous digit is either 9 or 0).
I hope this is useful to someone.
oppScore = 22325
deduct = 500
oppScoreStr$ = str(oppScore)
digitCount = len(oppScoreStr$)
oppName$ = "Operations"
nameTxt = CreateText(oppName$)
SetTextSize(nameTxt , 3)
nameTxtH# = GetTextTotalHeight(nameTxt)
txtY# = 50 - 0.5 * nameTxtH#
nameTxtW# = GetTextTotalWidth(nameTxt)
nameTxtX# = 50 - nameTxtW#
SetTextPosition(nameTxt , 50 - nameTxtW# , txtY#)
tempTxt = CreateText("0")
SetTextSize(tempTxt , 3)
chW# = GetTextTotalWidth(tempTxt)
DeleteText(tempTxt)
dim digits[digitCount]
for i = 1 to digitCount
digits[i] = CreateText(mid(oppScoreStr$ , i , 1))
SetTextSize(digits[i] , 3)
digitX# = nameTxtX# + nameTxtW# + chW# + i * chW#
SetTextAlignment(digits[i] , 1)
SetTextPosition(digits[i] , digitX# , txtY#)
next i
dim counterDigits[digitCount , 9]
for i = 1 to digitCount
x# = GetTextX(digits[i])
for j = 0 to 9
valScoreDigit = val(GetTextString(digits[i]))
if j = valScoreDigit
counterDigits[i,j] = digits[i]
else
counterDigits[i,j] = CreateText(str(j))
SetTextSize(counterDigits[i,j] , 3)
SetTextAlignment(counterDigits[i,j] , 1)
if j - valScoreDigit = 1
y# = txtY# - nameTxtH# * (j - valScoreDigit)
else
if j < valScoreDigit
y# = txtY# + nameTxtH# * ( valScoreDigit - j)
elseif j > valScoreDigit + 1
place = 10 - j + valScoreDigit
y# = txtY# + nameTxtH# * place
endif
endif
SetTextPosition(counterDigits[i,j] , x# , y#)
endif
SetTextVisible(counterDigits[i,j] , 0)
SetTextScissor(counterDigits[i,j] , 0 , txtY# , 100 , txtY# + GetTextTotalHeight(nameTxt))
next j
next i
showDigit1 = val(GetTextString(digits[digitCount]))
for d = 1 to deduct
showDigit1 = RotateDigitsInc(digitCount , showDigit1 , nameTxtH#)
if digitCount - 1 > 0 and showDigit1 = 0
if digi2once = 0
digi2once = 1
showDigit2 = val(GetTextString(digits[digitCount - 1]))
endif
showDigit2 = RotateDigitsInc(digitCount - 1 , showDigit2 , nameTxtH#)
if digitCount - 3 > 0 and showDigit2 = 0
if digi3once = 0
digi3once = 1
showDigit3 = val(GetTextString(digits[digitCount - 2]))
endif
showDigit3 = RotateDigitsInc(digitCount - 2 , showDigit3 , nameTxtH#)
if digiCount - 4 > 0 and showDigit3 = 0
if digi4once = 0
digi4once = 1
showDigit4 = val(GetTextString(digits[digitCount - 3]))
endif
showDigit4 = RotateDigitsInc(digitCount - 3 , showDigit4 , nameTxtH#)
endif
endif
endif
next d
do:sync():loop
END
function RotateDigitsDec(iDigitPlace , iStartDigit , dY#)
numToRotate = mod(iStartDigit + 1 , 10)
rotateTo = mod(iStartDigit + 2 , 10)
numToMove = numToRotate
maxPct = 120
stepSize = 40
numSteps = maxPct / stepSize
for pct = 1 to maxPct step stepSize
for j = 0 to 9
txtID = counterDigits[iDigitPlace , numToMove]
newY# = GetTextY(txtID) - dY# / numSteps
SetTextY(txtID , newY#)
numToMove = numToMove - 1
if numToMove = -1 then numToMove = 9
next j
Sync()
next pct
rotateY# = GetTextY(counterDigits[iDigitPlace , rotateTo]) + dY#
SetTextY(counterDigits[iDigitPlace , numToRotate] , rotateY#)
showDigit = iStartDigit - 1
if showDigit = -1 then showDigit = 9
endfunction showDigit
function RotateDigitsInc(iDigitPlace , iStartDigit , dY#)
numToRotate = mod(iStartDigit + 2 , 10)
rotateTo = mod(iStartDigit + 1 , 10)
numToMove = numToRotate
maxPct = 100
stepSize = 100
numSteps = maxPct / stepSize
for pct = 1 to maxPct step stepSize
for j = 0 to 9
txtID = counterDigits[iDigitPlace , numToMove]
newY# = GetTextY(txtID) + dY# / numSteps
SetTextY(txtID , newY#)
numToMove = mod(numToMove + 1 , 10)
next j
Sleep(10)
Sync()
next pct
rotateY# = GetTextY(counterDigits[iDigitPlace , rotateTo]) - dY#
SetTextY(counterDigits[iDigitPlace , numToRotate] , rotateY#)
showDigit = mod(iStartDigit + 1 , 10)
endfunction showDigit
function WriteDebug(text$)
dim debugLog[1]
debugLog[0] = OpenToWrite("debug_log.csv" , 1)
if toggleTimeStamp = 1
time$ = Str(Getmilliseconds())
addZeros = 6 - len(time$)
if addZeros > 0
for i = 1 to addZeros
zero$ = "0" + zero$
next i
endif
time$ = zero$ + time$
text$ = time$ + " -- " + text$
endif
if toggleTimeStamp = 2
text$ = GetCurrentDate() + " " + GetCurrentTime() + ": " + text$
endif
if len(text$) = 0 then text$ = ""
writeLine(debugLog[0],text$)
closeFile(debugLog[0])
endfunction