2012-08-16 73 views
2

我在Flash中有一個'InvZone'類來擴展精靈。這個想法是這個類將創建一個Sprite子元素的數組,併爲它們計算和分配x,y,width和height屬性,然後在行中添加它們(在參數中指定多少行和子元素)。AS3將子項添加到精靈影響精靈尺寸......很有趣

精靈(invZone)及其子節點(invWindow)都在Flash庫中。在那裏他們有添加符號來提供背景,並確保它們的初始起始維度不爲0.

我的問題是 - 添加子節點的行爲似乎是拋出InvZone實例的高度和寬度值。我希望如果孩子(行列總數)超過了InvZone的初始維度,但是我的代碼應該防止這種情況發生(除非我在某些問題上對代碼盲目進行處理)。

下面是類代碼:

package pnc.gui { 

import flash.display.Sprite; 
import pnc.gui.InvWindow; 

public class InvZone extends Sprite { 

    public const PADDING_X:int = 5 
    public const PADDING_Y:int = 5 
    public var invWindowArray = new Array(); 
    public var invArray = new Array(); 
    private var windows:int; 
    private var rows:int; 

    public function InvZone(inputX:int, inputY:int, inputHeight:int, inputWidth:int, inputWindows:int, inputRows:int) { 
     //normally these would set by args, but today we'll set them by hand 
     height = 100; 
     width = 600; 
     x=0; 
     y=0; 
     windows = 16 
     rows = 3 

     init() 
    } 

    private function init() { 
     var windowsPerRow:int = Math.ceil(windows/rows); 
     var rowHeight:Number = (height - (2*PADDING_Y))/rows; 
     var columnFullWidth:Number = (width - (2*PADDING_X))/windowsPerRow 


     for (var i = 0; i<windows; i++) { 
      var currentRow:int = Math.floor(i/windowsPerRow) 
      var invWindow:InvWindow = new InvWindow(); 
      invWindowArray.push(invWindow); 

      invWindowArray[i].x = (columnFullWidth * (i%windowsPerRow)) + PADDING_X; 
      //trace("X:"+invWindowArray[i].x) 

      invWindowArray[i].y = (rowHeight * currentRow) + PADDING_Y; 
      //trace("Y:"+invWindowArray[i].y) 

      invWindowArray[i].height = rowHeight - PADDING_Y; 
      //trace("HEIGHT:"+invWindowArray[i].height) 

      invWindowArray[i].width = columnFullWidth - PADDING_X; 
      //trace("WIDTH:"+invWindowArray[i].width) 

      trace(" ContainingSpriteHeight: " + height + " rowHeight: " + rowHeight + " currentRow: " + currentRow); 
      if (invWindowArray[i]) { 
       addChild(invWindowArray[i]); 
      } 
     } 
    }  
} 
} 

這裏是跟蹤:

ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 0 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 0 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 0 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 0 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 0 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 0 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 1 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 1 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 1 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 1 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 1 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 1 
ContainingSpriteHeight: 100 rowHeight: 30 currentRow: 2 
ContainingSpriteHeight: 128.55 rowHeight: 30 currentRow: 2 
ContainingSpriteHeight: 128.55 rowHeight: 30 currentRow: 2 
ContainingSpriteHeight: 128.55 rowHeight: 30 currentRow: 2 

所以最後一行是某種推擠包含Sprite的 - 但我很茫然,以這是怎麼發生的。高度不是唯一受影響的值 - 包含的寬度可以用不同的變量吹出。發生這種情況時,所有InvWindow精靈都會一致地改變大小,但不會改變庫中的原始背景符號。我試過轉移聲明並添加了ADDED_TO_STAGE監聽器,但沒有任何改變。有人可以用線索棒打我嗎?

更新:澄清:我不認爲兒童的邊緣應該超過含有精靈的邊界 - 所以AFAIK它不應該擴展。桑切斯已經修復了症狀,但是包含InvZone擴展和擴展InvWindows的原因是什麼?

UPDATE2與解決方案:代理下面桑切斯的建議,看看,看看有什麼可以被縮放 - 我認識的背景精靈加入到invZone在圖書館給它一個維度是縮放以意想不到的方式,打破了界限。通過在代碼中而不是在庫中添加背景精靈,給它提供h類的參數並讓它定義invZone的尺寸,問題就消失了。

回答

2

如果我明白了,你希望你的InvZone返回由你設置的高度和寬度,而不是根據內容計算出的beining?

在這種情況下,您可以覆蓋寬度和高度的getters以返回您的值。

替換:

height = 100; 
width = 600; 

有:

_h = 100; 
_w = 600; 

並添加:

private var _w; 
private var _h; 


override public function get height():Number 
{ 
    return _h; 
} 

override public function set height(value:Number):void 
{ 
    super.height = value; 
} 

override public function get width():Number 
{ 
    return _w; 
} 

override public function set width(value:Number):void 
{ 
    super.width = value; 
} 
+0

喜Sanchez157643 - 感謝您的答覆。這確實會限制包含的精靈,但是當窗口現在尺寸合適時,背景精靈現在已經縮小,所以窗口仍然溢出背景。我認爲首先創建invWindows佈局涉及的數學有些問題,但我無法將它放在手指上。我可以用你的新值改變背景符號的大小,但我很想知道它爲什麼會發生。 – BSBWebTeam 2012-08-16 01:50:42

+0

已更新,以澄清剩餘的查詢並通知@ Sanchez157643 – BSBWebTeam 2012-08-16 05:27:50

+0

this.scrollrect = new Rectangle(0,0,600,100); - 這將隱藏溢出。 – 2012-08-16 10:29:19