当我添加新元素时,正在删除数组元素(MovieClip)。为什么?

时间:2013-04-18 01:34:16

标签: arrays flash actionscript-2 movieclip

我在ActionScript 2.0中遇到了一些奇怪的行为。我正在尝试向舞台添加一定数量的页面(MovieClip),同时将引用存储在方便的数组中以供以后访问。这是我的代码(假设所有变量都已正确声明):

    _adPages = new Array();

    _adPages[0] = adPageTrack.attachMovie("adPage", "adpage0" + 0, getNextHighestDepth());
    _adPages[0].init( _adItems[0] );
    _adPages[0]._x = 0 * 10;

    _adPages[1] = adPageTrack.attachMovie("adPage", "adpage0" + 1, getNextHighestDepth());
    _adPages[1].init( _adItems[1] );
    _adPages[1]._x = 1 * 10;

    _adPages[2] = adPageTrack.attachMovie("adPage", "adpage0" + 2, getNextHighestDepth());
    _adPages[2].init( _adItems[2] );
    _adPages[2]._x = 2 * 10;

    _adPages[3] = adPageTrack.attachMovie("adPage", "adpage0" + 3, getNextHighestDepth());
    _adPages[3].init( _adItems[3] );
    _adPages[3]._x = 3 * 10;

    trace(_adPages);
    for (var i:Number = 0; i < 4; i++) {
        trace("Ad page x coordinate: " + _adPages[i]._x);
        trace("Ad page y coordinate: " + _adPages[i]._y);
    }

这原本是一个循环,它已经爆炸,所以我可以更仔细地检查它。请注意跟踪语句。他们应该输出一个MovieClip句柄数组,然后输出一个x和y坐标列表。相反,他们输出:

,,,_level0.main.adPageTrack.adpage03
Ad page x coordinate: undefined
Ad page y coordinate: undefined
Ad page x coordinate: undefined
Ad page y coordinate: undefined
Ad page x coordinate: undefined
Ad page y coordinate: undefined
Ad page x coordinate: 30
Ad page y coordinate: 0

除了要设置的最后一项之外的所有项目都已删除。如果我删除第四个项目,那么第三个项目现在完好无损,而其他所有项目都消失了。

有谁知道这里发生了什么?这是一个错误还是我错过了一些关于语言的东西?

1 个答案:

答案 0 :(得分:2)

附加movieclip时需要使用adPageTrack.getNextHighestDepth(),否则每个attachMovie调用都会删除前一个。

_adPages[0] = adPageTrack.attachMovie("adPage", "adpage0" + 0, adPageTrack.getNextHighestDepth());
[...]