Reading a text file in ActionScript 3

A problem I and many other Flash developers seem to run into is reading information from a text file. Or more specifically, reading co-ordinates for Sprites, from a text file. It seems that there are many examples of reading a file in ActionScript 3, but not many for reading co-ordinates for sprites, from a text file.

I recently made a level editor that exported sprites that I placed, into a text file. After searching through many posts and forums I found that most people used XML to store this kind of data. But seeing as my text file was quite simple, I really didnt want to use XML and decided that a normal text file was perfect. All the text file has in it is 2 values seperated by a comma. Each value represents the x and the y value for the sprite.

So here is an example of the text file.


As you can see the first value is the sprites X position and the second value is the sprites Y position. Quite simple.

Right so lets get coding.

The first thing we need to do, is embed the file. I prefer embedding any assets I have in the game, as it only requires an active internet connection once.

public class PlayState extends FlxState {
[Embed(source = "../assets/level1.txt", mimeType = "application/octet-stream")]
private var lvl1:Class;

Now what we basically need to do is read the file into a ByteArray. Then read the ByteArray and bobs yours uncle.

var barLevel:ByteArray;
//You'd normally place the following line, after you know what level your going to load
barLevel = new lvl1();
//Get file into a decent format
//bytesAvailable will pretty much read the whole file into memory.
var wholeLevel:String = barLevel.readMultiByte(barLevel.bytesAvailable, barLevel.endian);
//Split the wholeLevel string by a newline character
var arLines:Array = wholeLevel.split("\n");

var x:int = 0;
var y:int = 0;

The whole file is now in memory. NOTE: wholeLevel is a variable that holds the entire file, thats the reason we split it by newline. Each line is a set of co ordinates, for each sprite.
Additionally you’ll see I declare 2 variables there. These are the 2 variables that will hold the X and Y co-ordinates for the sprite.

So no we come to the part of the code that actually does the sprite generation. It might seem slightly complext, so I’ll explain everything nicely. The general idea here is that we are getting each line. Then getting the X and Y co-ordintates from each line.

Right so firstly loop through the “wholeLevel”.

for each (var singleLine:String in arLines)
    //More code coming up

The following lines will all go inside the for loop.

x = int(singleLine.subs tring(0, singleLine.indexOf(",")));

This line gets all the characters from the beginning of the string to the “,”(comma). Then converts that text into a Integer.

y = int(singleLine.sub string(singleLine.indexOf(",") + 1, singleLine.length));

This line gets all the characters from the “,”(comma) to the end of the line. NOTE: AS3 will not automatically get all the characters in the string, if you leave out the last argument. You must alway have the length.

grpBlocks.add(new FlxSprite(x, y, imgSprite));

Finally! Some sprite generation. This line will make a sprite with the co-ords from the text file. It will also add that sprite to a group. Groups are personally one of my favourite things about Flixel. Granted you dont have to use them, but they make things easier.

Now that you have all the required sprites inside a group. You can simply add the group.


That’s it. Generating sprites from a csv file. It’s very simple, but sometimes that’s all you need.

2 thoughts on “Reading a text file in ActionScript 3

  1. Very useful, thanks for posting. Just one thing, when you say: “//Split the wholeLevel string by a newline character
    var arLines:Array = wholeLevel.split(“n”);”, it should be “\n” instead of “n”.

Leave a Reply

Your email address will not be published. Required fields are marked *