AS3将子节点添加到精灵影响精灵维度......奇怪的是

时间:2012-08-16 00:47:46

标签: actionscript-3 flash flash-cs5

我在Flash中有一个'InvZone'类来扩展精灵。我们的想法是,这个类将创建一个Sprite子数组,计算并为它们分配x,y,width和height属性,然后将它们添加到行中(在参数中指定了多少行和子项)。

sprite(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

所以最后一行是以某种方式推动包含精灵 - 但我不知道这是怎么发生的。高度不是唯一受影响的值 - 包含的宽度可以用不同的变量吹出。发生这种情况时,所有InvWindow精灵都会一致地更改大小,但不会更改库中的原始背景符号。我试过转移声明和添加ADDED_TO_STAGE监听器,但没有变化。有人可以用线索棒打我吗?

更新:澄清:我不认为孩子的边缘应该超出包含精灵的边界 - 所以AFAIK根本不应该扩展。桑切斯已经修复了这个症状,但是包含InvZone的原因是什么?扩展和拉伸InvWindows?

UPDATE2与解决方案:根据Sanchez的建议在下面查看可能正在缩放的​​内容 - 我意识到在库中添加了invZone的背景精灵给它一个维度是以意想不到的方式缩放,打破界限。通过在代码中而不是在库中添加背景精灵,为它提供h&amp;类的参数。 w并让它定义了invZone的尺寸,问题就消失了。

1 个答案:

答案 0 :(得分:2)

如果我理解,您希望InvZone返回由您设置的高度和宽度,而不是根据内容进行计算?

在这种情况下,您可以覆盖宽度和高度的getter以返回值。

替换:

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;
}