字符串和文本字段,AS3

时间:2010-04-16 21:14:06

标签: flash actionscript-3

如何让我的文字字段正确填充并显示个位数?

描述
每个文本字段都接收一个子字符串。这不会限制它的输入,因为文本字段显示额外的数字。见插图。

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 有10个文本对象,每个文本对象都有一对文本字段。 它比Ex B复杂,因为它有一个Y动画和一对 数字。动画文本对象以创建滚动效果。它垂直移动,并且每个符号中都包含一个引号和一个追赶号码。有关更多说明,请参见插图。

alt text http://www.ashcraftband.com/myspace/videodnd/noob___.jpg alt text http://www.ashcraftband.com/myspace/videodnd/noob__.jpg

计数器设置为2,200,000.00,只是为了查看这些数字是否正在填充。

Ex B 工作正常!例如,只有

//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);
}

1 个答案:

答案 0 :(得分:1)

我只有机会快速查看您的代码,但我猜您可能错误地使用了substr()。我想我理解正确,你想从每个盒子里拿一个数字,从存储在String对象中的长数字。如果是这样,substr的工作方式如下:

substr(开头的字母,之后的字母数);

所以你看,如果你有一个10位数字的字符串(比如说1234567890),并且你做了那个String.substr(3,3),那么它将返回456,因为你是从索引3开始的(并且有也是一个0索引),你在起始索引之后包含3位数。因此,如果你只需要你的substr中的单个数字,你需要只做(3,1),(4,1),(5,1)等。

我希望这是有道理的,并解决问题。

得不