Sponsored By

AS3 Read a line of text from txt file.

I've ran into some problems when reading strings from text files so I wrote a simple function to do it for me. Hopefully others will either find this useful or have a better way of doing it.

Luke Manrodt, Blogger

May 19, 2012

1 Min Read

I've ran into some problems when reading strings from text files so I wrote a simple function to do it for me. Hopefully others will either find this useful or have a better way of doing it.

 

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

public static function ReadLine(data:ByteArray):String
{
    var eol:Boolean = false; //eol = end of line
    var str:String = "";

    while (data.bytesAvailable > 0 && !eol)
    {
        var char:String = data.readUTFBytes(1);
        if (char.charCodeAt(0) == '\n'.charCodeAt(0))
        {
            //Assert.AssertIf(false, str);
            eol = true;
        }
        else if (char.charCodeAt(0) == '\r'.charCodeAt(0))
        {
        }
        else
            str += char;
    }
   
    return str;
}

Read more about:

Blogs

About the Author(s)

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like