Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 TextField autoscroll to the bottom

How to autoscroll to the bottom of the TextField in ActionScript while adding text there programmatically:

var _output:TextField = new TextField();
for (var i:int = 0; i < 100; ++i) {
    _output.appendText("Hello World!");
}

Also consider that the vertical scrolling of the TextField should be enabled, and once a new text was added then autoscroll to the bottom should be executed again.

like image 529
Narek Avatar asked Aug 13 '13 10:08

Narek


1 Answers

You should listen for Event.CHANGE event on the TextField in question. Event description relative to textField And if you capture this event, you play with scrollV property. Say, like this:

_output.addEventListener(Event.CHANGE,scrollAllDown);
function scrollAllDown(e:Event):void {
    var tf:TextField=(e.target as TextField);
    if (!tf) return; 
    tf.scrollV=tf.maxScrollV;
}

Update: Catching Event.CHANGE does not work, I leave this in case someone stumbles on this method and too finds out it doesn't work. So, the only way is to subclass the TextField and manually override appendText() method to include scrolling, like this:

public class OutputTF extends TextField 
{ 
    // constructor omitted
    override public function appendText(text:String):void 
    { super.appendText(text); this.scrollV=this.maxScrollV; } 
}
like image 63
Vesper Avatar answered Oct 15 '22 21:10

Vesper