vts
vts

Reputation: 1717

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

Upvotes: 0

Views: 947

Answers (1)

bitmapdata.com
bitmapdata.com

Reputation: 9600

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.

enter image description here

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

Upvotes: 1

Related Questions