2016-12-12 34 views
1

我正在编写一个非常简单的程序,可以让你在一个圆周上移动,同时在舞台上还有一个矩形。当你拖动它时,我想让这个圆圈在矩形前面,但是当你释放鼠标时,这个圆圈将被送回。如何在as3中使用getChildIndex设置公共变量?

我不知道如何使用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”)。

问题是在我设置在开始的公共var,它不让我得到myCircle的子索引,但如果我把一个函数内的同一行,它的作品。
我知道我可以直接将myCircle的索引号放在最后一行(并删除公共var myCircleIndex),但我发现有一种方法可以将getChildIndex用于类中的公共var。

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

+0

所有你需要做的,把广场后面的圆圈上释放做的addChild(方形)或addChildAt(圆圈,0 ); –

+0

通过试图追踪我认为的变量,您就过于复杂了。让闪光灯在幕后进行排序。 –

+0

如果你想多一点技巧,并且想把圆圈直接放在正方形的后面(如果有100层并且正方形在12级,但你不确定正方形在哪个级别),你可以做“ addChildAt(circle,getChildIndex(square)-1);' –

回答

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); 
    } 
} 
+0

谢谢你回答我的问题,真的帮了我很多:) – m5signorini

2

所有你需要做的,把广场后面的圆圈上释放做addChild(myRectangle)addChildAt(myCircle, 0);

您试图追踪在我看来,一个变量过于复杂的事情。让闪光灯在幕后进行排序。

如果你想要多一点技巧,并且想把圆直接放在正方形的后面(如果有100层并且正方形在12级,但你不确定正方形在哪个级别)可以做

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

setChildIndex(myCircle,numChildren的-1);

这样做很好。执行此操作的更常见方法仅为

addChild(myCircle); 

它完全相同。很多人都被这种想法所困惑,这会增加一个新的myCircle,但如果它已经在显示列表中,并且它不在显示列表中,它会将它添加到显示列表顶部的z-订单(numChildren-1)。

+0

这是一个很好的答案,但可能有很多原因需要跟踪圆的z-索引。例如,如果该圆有其他的兄弟姐妹(除矩形之外),那么在不被拖动时它需要在后面。不确定你是否有足够的信息来推断它过于复杂。 – BadFeelingAboutThis