AS3 setChildIndex на фронт

Есть ли способ отправить определенный мувиклип впереди всех остальных мувиклипов на сцене?

Я знаю о setChildIndex, но я не могу найти способ динамического расчета верхней позиции.

5 ответов

Решение

Ты можешь использовать setChildIndex() с numChildren,

setChildIndex(childClip, numChildren - 1);

Вам не нужно использовать setChildIndex. addChild отправляет дочерний элемент поверх всех клипов. Вы можете подумать, что addChild дважды добавит ваш MC, но это не так, во второй раз он просто обновит свой дочерний индекс.

По умолчанию flash добавляет мувиклип в начало списка отображения, поэтому вы можете просто повторить добавление дочернего элемента в контейнер.

stage.addChild(bottom);
stage.addChild(top);
// top is now on bottom.

stage.addChild(bottom);
// bottom is now on top.

Мувиклип (mc) добавляется на холст,

canvas.setChildIndex(mc, 0);

Холст добавляется на сцену,

stage.setChildIndex(canvas, 0);
/* Bring Any Clicked Object to the Front
Clicking on any symbol on the Stage moves it in front of all other instances.
*/

// This code makes all symbol instances on stage clickable by making them listen for the CLICK event.
for (var fl_ChildIndex:int = 0;
    fl_ChildIndex < this.numChildren;
    fl_ChildIndex++)
{
    this.getChildAt(fl_ChildIndex).addEventListener(MouseEvent.CLICK, fl_ClickToBringToFront);
}

// This is the function that moves the clicked object to the front of the display list

function fl_ClickToBringToFront(event:MouseEvent):void
{
    this.addChild(event.currentTarget as DisplayObject);
}

От Adobe Flash Professional Фрагменты кода.

Другие вопросы по тегам