AS3 Switch conditions being ignored

So recently I've been working on a game in Flash, using the program Flash Builder and the Flixel engine. I hit a strange snag though, one of the first things that runs is a parser I made to create the screen for each level based on a 2D array in a .txt file, and for some reason, the final letter in each line is being ignored in the switch statement that decides what that index stands for.

This is the part I'm having issues with (what happens in each condition is irrelevant, I just left it in for context, but they all behave correctly):

var row:int;
var column:int;
var currentColor:int = 0;
for (row = 0; row < m_levelData.length; row++) {
    for (column = 0; column < m_levelData[0].length; column++) {
        var offset:FlxPoint = new FlxPoint(0, 0);
        var thisIndex:String = m_levelData[row][column];
        //trace("This Line is " + m_levelData[row]);
        trace("Current index is " + thisIndex);

        //trace(row + " " + column);
        switch(thisIndex) {

            case Statics.GRID_BIN:
                offset.y = (spaceSize.y / Statics.SPRITE_SCALE - BinFront.SIZE.y / Statics.SPRITE_SCALE) / 2;
                offset.x = (spaceSize.x/Statics.SPRITE_SCALE - BinFront.SIZE.x/Statics.SPRITE_SCALE) / 2;
                var color:uint;
                if (m_colors.length < currentColor) {
                    color = Statics.COLOR_BLACK;
                    trace("No color found");
                } else {
                    color = m_colors[currentColor];
                    trace("Color is " + color);
                makeBin(spaceSize.x * column + offset.x + levelOffset.x, spaceSize.y * row + offset.y + levelOffset.y, color);

            case Statics.GRID_BUILDER:
                offset.y = (spaceSize.y/Statics.SPRITE_SCALE - BuilderMain.SIZE.y/Statics.SPRITE_SCALE)/2
                offset.x = (spaceSize.x/Statics.SPRITE_SCALE - BuilderMain.SIZE.x/Statics.SPRITE_SCALE) / 2;
                makeBuilder(spaceSize.x * column + offset.x + levelOffset.x, spaceSize.y * row + offset.y + levelOffset.y);

            case Statics.GRID_CONVEYOR:
                var length:int = 0;
                if (column == 0 || m_levelData[row][column - 1] != Statics.GRID_CONVEYOR) {
                    for (i = column; i < m_levelData[row].length; i++) {
                        if (m_levelData[row][i] == Statics.GRID_CONVEYOR) {
                        } else {

                    offset.y = (spaceSize.y/Statics.SPRITE_SCALE - Statics.GRID_SIZE.y/Statics.SPRITE_SCALE)/2
                    offset.x = (spaceSize.x/Statics.SPRITE_SCALE - Statics.GRID_SIZE.x/Statics.SPRITE_SCALE) / 2;
                    makeConveyor(spaceSize.x * column + offset.x + levelOffset.x, spaceSize.y * row + offset.y + levelOffset.y, length);

                trace("Nothing at this index, it was " + thisIndex);

Statics.GRID_BIN, Statics.GRID_CONVEYOR, and Statics.GRID_BUILDER are all constant strings ("a", "b", and "c" respectively), and I know that this all should be working because it was working before I switched to the parser. Now, your immediate response to that is that the problem is my parser, but I have suspicions that something is a little screwy other than that. Before the switch, I print the value of thisIndex..(trace("Current index is " + thisIndex);), and whenever it is the last letter in a line that was parsed (using split(","), even when it matches one of the switch conditions, the default condition is run and nothing happens.

Has anyone else seen something like this, or am I just making a really dumb mistake?


When you parse and compare strings loaded from a .txt you have to deal with various "hidden" characters. At line endings you might run into CR (\r) LF(\n) or both or the other way round. It can get a bit fiddly to find out which applies to your system or .txt.

In the end you could probably make it work using String.replace() .

This has been discussed for example here: How do I detect and remove "\n" from given string in action script?

Apart from this it's hard to judge whether there's something wrong with your parser. Your use of the switch{}-statement itself looks fine and should be working.

If you need to debug the code try feeding it with a hardcoded const :String instead of the .txt-file. Then set a breakpoint (in Flash Builder by double-clicking the line number next to the switch-statemen) and step through it line by line.

Need Your Help

XSL Transformation not taking xml

c# xml xslt

I'm trying to transform an XML string with a XSL stylesheet. But for some reason he only takes my root element and not the rest. Something wibbly wobbly timey wimey probs but I can't see the issue....

R generate 2D histogram from raw data

r 2d histogram raw-data

I have some raw data in 2D, x, y as given below. I want to generate a 2D histogram from the data. Typically, dividing the x,y values into bins of size 0.5, and count the number of occurrences in ea...