I have a 'InvZone' class in flash that extends sprite. The idea is that this class will create an array of Sprite children, compute and assign x,y,width and height properties to them and then addChild them in rows (how many rows and children being specified in the arguments).
Both the sprite (invZone) and its children (invWindow) are in the Flash library. There they have symbols added to provide backgrounds and make sure their initial starting dimension are not 0.
My problem is - the act of adding the children seems to be throwing out the height and width values of the InvZone instance. I would expect that if the children (in row and column totals) somehow exceeded the initial dimensions of the InvZone, but my code should be preventing that (unless I am totes codeblind on some issue).
Here is the class code:
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]);
}
}
}
}
}
And here is the trace:
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
So the last row is somehow pushing against the containing sprite - but I am at a loss as to how this happens. Height is not the only value affected - the containing width can be blown out with different variables. When this occurs, all the InvWindow sprites change size consistently but not the original background symbol from the library. Ive tried shifting declarations and adding ADDED_TO_STAGE listeners, but there is no change. Can someone hit me with a clue stick, please?
UPDATE: To clarify: I don't think the children's edges should exceed the boundaries of the containing sprite - and so AFAIK it shouldn't expand at all. Sanchez has fixed the symptom, but what's the cause of the containing InvZone expanding and stretching the InvWindows?
UPDATE2 with solution: Acting on Sanchez's suggestion below to look to see what might be scaling - I realised the background sprite added to invZone in the library to give it a dimension was scaling in unexpected ways, breaking the boundaries. By adding the background sprite in code rather than in the library, giving it the arguments for the class for h & w and letting it define invZone's dimensions, the problem went away.