actionscript-3flashlinkagedisplayobject

Why do these changes in object.name result in different displays?


I was doing something simple - drawing a series of tiles, placing them in the library with linkage in the properties, and creating a large rectangle of randomly chosen tiles. I also wanted to be able to access each tile independently, so I used an array-type naming system for each tile. I then came across this situation where some tiles would not be placed in the designated x,y location (seems to end up at 0,0 instead) if I use a naming scheme where the variables are next to each other (as opposed to splitting them up with a string-literal).

Can someone please explain why: floor.name = "floorR"+String(n)+"C"+String(m); //behaves properly, as expected

gives different display results from: floor.name = "floorRC"+String(n)+String(m); //converted to a comment in the code below, tends to misplace the tile

in the following minimum, reproducible extract:

//initialize standard counters
var i:uint = 0;
var j:uint = 0;
var m:uint = 0;
var n:uint = 0;
var lvlAdd:uint = 0; //designates add level (display)


//starting location of tiles
var bigMapX0:uint = 15;
var bigMapY0:uint = 75;

//dimensions of completed tile set
var bigMapWidth:uint = 375;
var bigMapHeight:uint = 675;


//initialize max row and column
var rowMax = 25;
var columnMax = 15;

//initialize unit-cell dimensions
var cellWidth = bigMapWidth/columnMax;
var cellHeight = bigMapHeight/rowMax;

//backdrop rectangle: black
var backFieldColor = 0x000000;
var backField:Shape = new Shape();
backField.graphics.beginFill(backFieldColor);
backField.graphics.drawRect(bigMapX0,bigMapY0,bigMapWidth,bigMapHeight); //dimensions of standard stage
backField.graphics.endFill();
this.addChildAt(backField,lvlAdd); //sets black as bottommost static backdrop

//set down tiles
var floor:DisplayObject;
var floorName:DisplayObject;
for (m=0; m<columnMax; m++) {
    for (n=0; n<rowMax; n++){
        floor = new Floor0(); //"floor" is in the library with linkage named Floor0 (part of an assortment of tiles randomly chosen, and not shown here to reduce lines of code
        //floor.name = "floorR"+String(n)+"C"+String(m); 
        floor.name = "floorRC"+String(n)+String(m); //error happens if this line is used instead of the commented line above it
        lvlAdd++;
        this.addChildAt(floor, lvlAdd);
        //floorName = this.getChildByName("floorR"+String(n)+"C"+String(m));
        floorName = this.getChildByName("floorRC"+String(n)+String(m)); //this is the paired statement with the floor.name 3 lines prior, and causes an error in display if used instead of the commented line above it
        floorName.x = bigMapX0 + m*cellWidth;
        floorName.y = bigMapY0 + n*cellHeight;
    }
}

this.stop();

this is with the error

this is when "floorR"+m+"C"+n is used Thank you for your time.


Solution

  • Because this one

    "floorRC"+String(n)+String(m);
    

    will produce the same results for different pairs of n and m in certain cases, for example for

    The other one

    "floorR"+String(n)+"C"+String(m);
    

    will always produce a unique String.