代码之家  ›  专栏  ›  技术社区  ›  anon255058

字符串和文本字段,as3

  •  0
  • anon255058  · 技术社区  · 16 年前

    如何使文本字段正确填充并显示个位数?

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

    EXA

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

    EXA 有10个文本对象,每个对象都有一对文本字段。 它的移动比ex b复杂,因为它有一个y动画和一对 数字。文本对象设置动画以创建滚动效果。它垂直移动,每个符号中包含一个前导号和一个补码。有关更多说明,请参见图。

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

    计数器设置为2200000.00,只是为了查看数字是否正在填充。

    前缀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 回复  |  直到 16 年前
        1
  •  1
  •   debu    16 年前

    我只是有机会快速浏览一下您的代码,但我猜您可能使用了substr()错误。我想我理解得没错,你想从一个字符串对象中存储的长数字中得到每个框中的一个数字。如果是,substr的工作方式如下:

    substr(起始字母,后面的字母数);

    所以你看,如果你有一个10位数的字符串(比如1234567890),你做这个字符串。substr(3,3),它会返回456,因为你是从索引3开始的(还有一个0索引),你在开始索引后包含3位数。所以如果你只想从你的子序列中得到个位数,你只需要做(3,1),(4,1),(5,1),等等。

    我希望这是有意义的,并解决了问题。

    调试