Question

I have a very busy chat app and the textArea is set to clear itself after certain amount of text, but what I want is to show the last 100 lines of text in the textArea and remove the rest.

Using Flash CC

The text comes from a Red5 Server like this:

function onReceiveMsg(UserName:String, Msg:String):void{
  if (myTextArea.length > 21400){
    myTextArea.htmlText = "";
  }

   //add text to textArea
   myTextArea.htmlText += "<font color='#FF0000'>"+UserName+": "+Msg"</font>";

}

So, any idea to maintain the last 100 lines of chat? Thanks! :-)

Was it helpful?

Solution

I would simply store all the messages you receive in an array:

var messages:Array = [];

function onReceiveMsg(userName:String, msg:String):void
{
    messages.push({ userName: userName, msg: msg });
    displayMessages(); // See below.
}

And pull the last 0-100 items from that for display:

function displayMessages():void
{
    myTextArea.htmlText = "";

    var recent:Array = messages.slice(-100);
    for each(var i:Object in recent)
    {
        myTextArea.htmlText += "<font color='#FF0000'>" + i.userName + ": " + i.msg + "</font>";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top