setChildIndex在重叠按钮上

时间:2012-11-22 12:45:52

标签: actionscript-3

舞台上有两个按钮,彼此叠加。我试图使用setChildindex,这样当我按下开启按钮时,关闭按钮将出现在它上面。反之亦然。但我一直得到这个错误,我似乎无法理解。

追踪     ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子级。     在flash.display :: DisplayObjectContainer / setChildIndex()     在com.audio::Sequencer/trackOn1()

private var trackOnOff:Boolean = true;

this.track1.trackOn1.addEventListener(MouseEvent.CLICK, trackOn1);
this.track1.trackOff1.addEventListener(MouseEvent.CLICK, trackOff1);

public function trackOn1(event:MouseEvent = null):void
{
trace("Track On")
this.setChildIndex(this.track1.trackOff1, this.numChildren - 1);
trackOnOff = true;
}

public function trackOff1(event:MouseEvent = null):void
{
trace("Track Off")
this.setChildIndex(this.track1.trackOn1, this.numChildren - 1);
trackOnOff = false;
}

请帮忙。

2 个答案:

答案 0 :(得分:0)

我希望您的track1成为包含资源的MovieClip,即trackOff1trackOn1。如果是这样,您必须将setChildIndex()来电定位到该MC,而不是this

public function trackOn1(event:MouseEvent = null):void
{
    trace("Track On")
    this.track1.setChildIndex(this.track1.trackOff1, this.track1.numChildren - 1);
    trackOnOff = true;
}

public function trackOff1(event:MouseEvent = null):void
{
    trace("Track Off")
    this.track1.setChildIndex(this.track1.trackOn1, this.track1.numChildren - 1);
    trackOnOff = false;
}

答案 1 :(得分:0)

有几种简单的方法可以做到这一点:

swapChildren(track1, track2);

两个按钮都可以调用这个函数,它与使用setChildIndex交换它们具有相同的效果,或者简单地调用addChild(track1)addChild(track2)将movieclip带到同一个函数前面层