簡體   English   中英

為什么 object.name 中的這些更改會導致不同的顯示?

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

我正在做一些簡單的事情 - 繪制一系列瓷磚,將它們放在庫中並在屬性中鏈接,並創建一個由隨機選擇的瓷磚組成的大矩形。 我還希望能夠獨立訪問每個圖塊,因此我為每個圖塊使用了數組類型的命名系統。 然后我遇到了這種情況,如果我使用變量彼此相鄰的命名方案(而不是拆分他們用字符串文字)。

有人可以解釋為什么: floor.name = "floorR"+String(n)+"C"+String(m); //行為正常,如預期的那樣

給出不同的顯示結果: floor.name = "floorRC"+String(n)+String(m); //轉換為下面代碼中的注釋,往往會放錯瓷磚

在以下最低限度的、可重現的提取物中:

//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();

這是錯誤

這是使用 "floorR"+m+"C"+n 的時候 感謝您的時間。

  • 格雷格

因為這個

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

在某些情況下,對於不同的nm對會產生相同的結果,例如

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

另一個

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

將始終產生一個唯一的String

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM