strings and textfields, AS3
- by VideoDnd
How do I get my text fields to populate correctly and show single digits?
Description
Each textfield receives a substring. This doesn't limit it's input, because the text fields shows extra numbers. See illustration.
Ex A
//Tweening method 'could substitute code with Tweener'
import fl.transitions.Tween;
import fl.transitions.easing.*;
//Timer that will run a sec and repeat
var timer:Timer = new Timer(1000);
//Integer values
var count:int = +220000000;
var fcount:int = 0;
//Events and starting timer
timer.addEventListener(TimerEvent.TIMER, incrementCounter);
addEventListener(Event.ENTER_FRAME, checkOdometerPosition);
timer.start();
//Tween Variables
var smoothLoop:int = 0;
var originalYPosition:Number = 0;
var upwardYPosition:Number = -99;
//Formatting String
function formatCount(i:int):String {
var fraction:int = i % 100;
var whole:int = i / 100;
return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction);
}
//First Digit 'trigger set by using var upwardPosition as a constant'
function checkOdometerPosition(event:Event):void{
if (seconds9.y <= upwardYPosition){
var toText:String = formatCount(fcount);
//seconds9.firstDigit.text = formatCount(fcount);
seconds9.firstDigit.text = toText.substr(9, 9);
seconds9.y = originalYPosition;
seconds8.firstDigit.text = toText.substr(8, 8);
seconds8.y = originalYPosition;
seconds7dec.firstDigit.text = toText.substr(7, 7);
seconds7dec.y = originalYPosition;
seconds6.firstDigit.text = toText.substr(6, 6);
seconds6.y = originalYPosition;
seconds5.firstDigit.text = toText.substr(5, 5);
seconds5.y = originalYPosition;
seconds5.firstDigit.text = toText.substr(4, 4);
seconds5.y = originalYPosition;
seconds3.firstDigit.text = toText.substr(3, 3);
seconds3.y = originalYPosition;
seconds2.firstDigit.text = toText.substr(2, 2);
seconds2.y = originalYPosition;
seconds1.firstDigit.text = toText.substr(1, 1);
seconds1.y = originalYPosition;
seconds1.firstDigit.text = toText.substr(1, 1);
seconds1.y = originalYPosition;
seconds0.firstDigit.text = toText.substr(0, 1);
seconds0.y = originalYPosition;
}
}
//Second Digit
function incrementCounter(event:TimerEvent):void{
count++;
fcount=int(count)
if (smoothLoop < 9){
smoothLoop++;
}
else {
smoothLoop = 0;
}
var lolly:String = formatCount(fcount-1);
//seconds9.secondDigit.text = formatCount(fcount);
seconds9.secondDigit.text = lolly.substr(9, 9);
var addTween9:Tween = new Tween(seconds9, "y", Strong.easeOut,0,-222, .7, true);
seconds8.secondDigit.text = lolly.substr(8, 8);
var addTween8:Tween = new Tween(seconds8, "y", Strong.easeOut,0,-222, .7, true);
seconds7dec.secondDigit.text = lolly.substr(7, 7);
var addTween7dec:Tween = new Tween(seconds7dec, "y", Strong.easeOut,0,-222, .7, true);
seconds6.secondDigit.text = lolly.substr(6, 6);
var addTween6:Tween = new Tween(seconds6, "y", Strong.easeOut,0,-222, .7, true);
seconds5.secondDigit.text = lolly.substr(5, 5);
var addTween5:Tween = new Tween(seconds5, "y", Strong.easeOut,0,-222, .7, true);
seconds4.secondDigit.text = lolly.substr(4, 4);
var addTween4:Tween = new Tween(seconds4, "y", Strong.easeOut,0,-222, .7, true);
seconds3.secondDigit.text = lolly.substr(3, 3);
var addTween3:Tween = new Tween(seconds3, "y", Strong.easeOut,0,-222, .7, true);
seconds2.secondDigit.text = lolly.substr(2, 2);
var addTween2:Tween = new Tween(seconds2, "y", Strong.easeOut,0,-222, .7, true);
seconds1.secondDigit.text = lolly.substr(1, 1);
var addTween1:Tween = new Tween(seconds1, "y", Strong.easeOut,0,-222, .7, true);
seconds0.secondDigit.text = lolly.substr(0, 1);
var addTween0:Tween = new Tween(seconds0, "y", Strong.easeOut,0,-222, .7, true);
}
Ex A has 10 text objects, each with a pair of text fields.
It’s move complex than Ex B, because it has a Y animation and pairs of
numbers. The text objects are animated to create a scrolling effect. It moves vertically, and has a lead number and a catch up number contained in each symbol. See illustration for more description.
The counters are set to 2,200,000.00, just to see if the numbers are populating.
Ex B work fine! for example only
//STRING SPLITTER COUNTER with nine individual text fields
//Timer settings
var delay:uint = 1000/100;
var repeat:uint = 0;
var timer:Timer;
timer = new Timer(delay,repeat);
timer.addEventListener(TimerEvent.TIMER, incrementCounter);
timer.start();
//Integer values
var count:int = 0;
var fcount:int = 0;
//Format Count
function formatCount(i:int):String {
var fraction:int = i % 100;
var whole:int = i / 100;
return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction);
}
//Split strings off to individual text fields
function incrementCounter(event:TimerEvent) {
count++;
fcount=int(count+220000000)
var toText:String = formatCount(fcount);
mytext9.text = toText.substr(9, 9);
mytext8.text = toText.substr(8, 8);
mytext7dec.text = toText.substr(7, 7);
mytext6.text = toText.substr(6, 6);
mytext5.text = toText.substr(5, 5);
mytext4.text = toText.substr(4, 4);
mytext3.text = toText.substr(3, 3);
mytext2.text = toText.substr(2, 2);
mytext1.text = toText.substr(1, 1);
mytext0.text = toText.substr(0, 1);
}
Here's a link to the files