0

我有这段代码可以完美运行,但偶尔——只要 Flash 这么认为——它就会崩溃。这是生成的调试文本:

错误 #2025

显示结果:真

多莉 = 10

多莉补充说

添加了通用芯片

高亮 SH:[object SurfaceElement]10-[object TSprite]-vex!

参数错误:错误 #2025

请注意“烦恼!” 是对象 vex 的名称它是一个 TSprite (=Sprite)。

    public function update(players:Array, showResult:Boolean = false):void {
        try {
            var debug:String = "showResult:" + showResult.toString() + "\n";
            debug += "dolly = " + dolly.toString();

            // RESET columns
            columns.length = 0;
            billboards.removeAllChildren();
            var column:ChipColumn;

            // add dolly
            var alfa:Number = 1;
            if (showResult) {
                column = getChipColumn(vex.getChildIndex(numberToSH[dolly]));
                column.addChild(dollySpr);
                alfa = LOSER_ALPHA; // fade-out (non-animated) non-winning columns
            }

            debug += "\nDolly added";

            for (var k:* in players) {
                var player:Player = players[k];
                var bets:Array = player.getBets();
                for (var i:* in bets) {

                    // is there a column already for these chips?
                    column = getChipColumn(i);
                    column.alpha = alfa; // loser..?
                    for (var j:int = bets[i]; j > 0; --j) {
                        var bmp:TBitmap = new TBitmap(chips[k], Align.CENTER, Align.CENTER);
                        bmp.y = - column.numChildren * 5;
                        column.addChild(bmp);
                    }
                }
            }

            debug += "\nGeneric Chips added";

            if (showResult) {
                var shs:Array = SurfaceElement.NumberToAllSH[dolly];
                for each (var sh:SurfaceElement in shs) {
                    debug += "\nHighlighting SH:" + String(sh);
                    debug += sh.corresponds.toString();
                    debug += "-" + String(sh.parent);
                    debug += "-" + String(sh.parent.name);

                    column = getChipColumn(vex.getChildIndex(sh)); // ERROR LINE ?!?
                    column.alpha = 1; // restore alpha
                    column.filters = [new GlowFilter(0xffffff, 1, 12, 12, 3, 3)];
                }
            }

            debug += "\nEnd of Highlighting !!!!!!";

            // sort columns by y
            var sorted:Array = columns.sortOn("sortY", Array.NUMERIC | Array.RETURNINDEXEDARRAY);
            debug += "\n" + sorted.toString();
            for (i in sorted) {
                column = columns[sorted[i]];
                if (!column) break; // pointers to undefined data have arrived (put last)
                billboards.addChild(column);
            }
            debug += "\nEnd of ALL";
        } catch(e:Error) {
            if (!Debug.field.text.length) {
                Debug.field.textColor = 0xffffff;
                Debug.field.appendText(e.message + "\n" + debug + "\n" + e.toString());
            }
        }
    }

    private function getChipColumn(i:int):ChipColumn {
        var column:ChipColumn = columns[i];
        if (!column) {
            column = new ChipColumn();

            // calc column properties
            var sh:SurfaceElement = vex.getChildAt(i) as SurfaceElement;
            var point:Point = sh.getCenterProjected();
            point = billboards.globalToLocal(point);

            column.sortY = sh.center.y;
            column.x = point.x;
            column.y = point.y;
            columns[i] = column;
        }
        return column;
    }

从调试文本中,很明显错误发生在这里:

debug += "\nHighlighting SH:" + String(sh);
debug += sh.corresponds.toString();
debug += "-" + String(sh.parent);
debug += "-" + String(sh.parent.name);
column = getChipColumn(vex.getChildIndex(sh)); // ERROR LINE ?!?

为非子对象调用 getChildIndex 时会发生错误 2025。但正如父母的名字所暗示的那样, sh vex 的孩子(vex!)。而且只有一个vex

另一个奇怪的事情是错误消息(e.message)只是产生: 错误 2025 而错误到字符串(e.toString())产生: ArgumentError: Error #2025

它不应该说得更像:

ArgumentError:错误 #2025:提供的 DisplayObject 必须是调用者的子对象??

4

1 回答 1

1

静态全局状态是一件坏事。在这种情况下,您无法知道从静态数组中检索的 sh 对象是否与当前的“vex”对象有任何关系。

与其SurfaceElement跟踪所有已添加的自身实例,不如考虑让您的Vex类公开一个表面元素数组(或者更好的是,向量)。如果这些是在舞台上构建的(我怀疑你正在静态跟踪它们,因为你没有一个很好的“处理”来处理时间线/舞台对象),那么你可以在你的构造函数中做这样的事情:

公共变量表面元素:数组= [];
函数 Vex() {
    极好的();
    var loops:int = numChildren;
    for (var i:int=0; i<loops; i++) {
       var surface:SurfaceElement = getChildAt(i) as SurfaceElement;
       如果(表面){
          surfaceElements[surfaceElements.length] = 表面;
       }
    }
}

然后,当你完成了那个特定的 Vex 时,你也完成了它的表面元素,没有交叉。

于 2013-06-03T16:59:24.833 回答