如何在as3中使用getChildIndex设置公共变量?

时间:2016-12-12 17:11:27

标签: class actionscript-3 var

我编写了一个非常简单的程序,可以让你围绕一个圆圈移动,在舞台上也有一个矩形。我想在拖动时让圆圈位于矩形前面,但是当你松开鼠标时,圆圈会被送回。

我不知道如何使用getChildIndex方法设置公共变量。我真的不关心其余的代码。我主要对如何使getChildIndex方法使用公共变量感兴趣。

package code
{

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Sprite;


    public class Main extends MovieClip
    {           
        public var myCircleIndex:int = getChildIndex(myCircle);

        public function Main()
        {
            myCircle.addEventListener(MouseEvent.MOUSE_DOWN, mouseClicking);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
        }

        public function mouseClicking(e:MouseEvent): void
        {
            myCircle.startDrag();
            setChildIndex(myCircle, numChildren-1);
        }

        public function mouseReleased(e:MouseEvent): void
        {
            myCircle.stopDrag();
            setChildIndex(myCircle, myCircleIndex);
        }
    }
}

我正在使用我在舞台上直接创建的实例(“myCircle”)作为影片剪辑。

问题在于我在开头设置的public var,它不会让我获得myCircle的子索引,但是如果我在函数中放入相同的行,它就可以工作。
我知道我可以直接将myCircle的索引号放在最后一行(并删除public var myCircleIndex),但我发现有一种方法可以将getChildIndex用于类中的public var。

如何在类中的公共变量中使用getChildIndex?

2 个答案:

答案 0 :(得分:3)

它不起作用的原因是因为当行public var myCircleIndex:int运行时,您的时间轴对象尚不存在。

由于这个原因,您不应该尝试访问类级变量声明中的非基本对象,因为在创建这些变量时,类中没有其他任何东西可用。

以下是如何重构此内容(请参阅代码注释):

public class Main extends MovieClip
{           
    public var myCircleIndex:int;  //just create the reference here, don't assign it
    public var myCircle:flash.display.DisplayObject; //this line is just for better compile time checking and code completion, completely optional

    public function Main()
    {
        //wait for all the display stuff to be created before trying to access it.  The constructor function can run before timeline stuff is created, so it's not safe to reference stage or timeline objects here. 
        if(!stage){
            this.addEventListener(Event.ADDED_TO_STAGE, timelineCreated);
        }else {
            timelineCreated(null);
        }
    }

    private function timelineCreated(e:Event):void {
        //now that we're certain the timeline stuff has been created, we can reference timeline objects and stage:

        //store the initial z-index of myCircle
        myCircleIndex = getChildIndex(myCircle);

        //the rest of your code that was in the construction -Main()- before
        myCircle.addEventListener(MouseEvent.MOUSE_DOWN, mouseClicking);
        stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
    }

    //no change to any of the following stuff

    public function mouseClicking(e:MouseEvent): void
    {
        myCircle.startDrag();
        setChildIndex(myCircle, numChildren-1);
    }

    public function mouseReleased(e:MouseEvent): void
    {
        myCircle.stopDrag();
        setChildIndex(myCircle, myCircleIndex);
    }
}

答案 1 :(得分:2)

您需要做的就是将广告放在正方形后面的是发布广告addChild(myRectangle)addChildAt(myCircle, 0);

在我看来,通过尝试跟踪变量来使事情过于复杂。让flash在幕后排序。

如果你想要更多的技巧,并且想要将圆圈直接放在正方形后面(如果有100层并且正方形处于12级,但你不确定正方形处于哪个水平)你可以做

addChildAt(myCircle, getChildIndex(myRectangle)-1);

注意

  

setChildIndex(myCircle,numChildren-1);

这样做很好。更常见的方法是

addChild(myCircle);

完全相同。许多人对这种想法感到困惑,这会添加一个新的myCircle,但如果它已经在显示列表中,它只会将它带到前面,如果它不在显示列表中,它会将它添加到显示列表中顶部的z顺序(numChildren-1)。