简体   繁体   中英

Flash AS3 how to set the index of a button on the stage?

I have about 60 buttons added manually to the timeline. I need to specify the index of each but the index is given depending on the order I drop the object on the stage from the library. I dont want to have to specify each button's index programmatically by name since there is no easy pattern to follow.

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

I can get all the indexes like this but they are not in the order I want. Thank you

To change the order of the index manually, use addChildAt (DisplayObject, index) if you want Two displayobject index switch. please use the following. swapChildren(DisplayObject1,DisplayObject2) swapChildrenAt(index1,index2)

If you look at the image below, there is the instance MovieClip 6 in stage. but the index is arbitrary. depends on really creating order or drag&drop order from libary. so i'm re-ordering index by name according to the order.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM