0

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.

  • Greg
Greg
  • 25
  • 3
  • What's the error? – Pranav Hosangadi Jul 30 '20 at 19:44
  • Using floor.name = "floorRC"+String(n)+String(m) (and the accompanying getChildByName) will place the 11th row: 0, 1, 2, 3, and 4th tiles elsewhere. Also the 21st row: 0, 1, 2, 3, 4th tiles. Using floor.name = "floorR"+String(n)+"C"+String(m) gives a complete 15 x 25 grid of tiles. – Greg Jul 30 '20 at 19:57
  • I'm not sure I understand. Can you add a screenshot of both? – Pranav Hosangadi Jul 30 '20 at 19:59
  • Added the two images. Thanks for looking into this. – Greg Jul 30 '20 at 20:13
  • I see nothing in the code that would do that. Maybe the code expects the name to be floorRC elsewhere in code? – Pranav Hosangadi Jul 30 '20 at 20:16
  • Also, the `getChildByName` seems unnecessary. Why can't you just set `floor.x` and `floor.y` before `this.addChild()`? – Pranav Hosangadi Jul 30 '20 at 20:18
  • I extracted the code given here and ran it and the images shown are the output ... so there is no other code elsewhere. Except for substituting the offending lines (converting the other lines into comments), the entire code is here - just need to add a rectangle in the library and name it "floor" and in properties activate linkage and name that Floor0. – Greg Jul 30 '20 at 20:29
  • I thought I had to name each unitCell instance from the library -- otherwise each tile would not be different from the other tiles and I would not be able to access each tile independently. – Greg Jul 30 '20 at 20:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218935/discussion-between-pranav-hosangadi-and-greg). – Pranav Hosangadi Jul 30 '20 at 20:33

1 Answers1

2

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

  • (n = 1, m = 12)
  • (n = 11, m = 2)

The other one

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

will always produce a unique String.

Organis
  • 7,243
  • 2
  • 12
  • 14