0

我在时间线上手动添加了大约 60 个按钮。我需要指定每个的索引,但索引的给出取决于我从库中将对象放在舞台上的顺序。我不想通过名称以编程方式指定每个按钮的索引,因为没有简单的模式可以遵循。

private function buttonHandler(event:MouseEvent):void
        {
            event.target.parent.getChildIndex(event.target));
}

我可以得到所有这样的索引,但它们不是我想要的顺序。谢谢

4

1 回答 1

1

要手动更改索引的顺序,请使用addChildAt (DisplayObject, index) 两个显示对象索引开关。请使用以下内容。swapChildren(DisplayObject1,DisplayObject2) swapChildrenAt(index1,index2)

如果您看下图,舞台中有实例 MovieClip 6。但索引是任意的。取决于真正从库中创建订单或拖放订单。所以我根据顺序按名称重新排序索引。

在此处输入图像描述

import flash.display.MovieClip;

var mc:MovieClip;
for(var i:int = 0; i<this.numChildren; i++)
{
    mc = this.getChildAt(i) as MovieClip;
    trace("before clipName: " + mc.name);
}

//re-ordering
for(i = 0; i<this.numChildren; i++)
{
    mc = this.getChildByName("mc"+i) as MovieClip;
    addChildAt(mc,i);
}

for(i = 0; i<this.numChildren; i++)
{
    mc = this.getChildAt(i) as MovieClip;
    trace("after clipName: " + mc.name);
}

console:

before clipName: mc1
before clipName: mc4
before clipName: mc3
before clipName: mc5
before clipName: mc2
before clipName: mc0

after clipName: mc0
after clipName: mc1
after clipName: mc2
after clipName: mc3
after clipName: mc4
after clipName: mc5
于 2012-08-21T02:10:42.697 回答