2012-08-06 30 views
0

这是我的代码。外部ActionScript 3.0 MOUSE_UP和可见

package core 
{ 
    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 
    import flash.geom.Point; 

    public class earth extends MovieClip 
    { 
     protected var position:Point = new Point(x, y); 
     public function earth() 
     { 
      stage.earthText_mc.visible = false; //HAVING PROBLEM WITH THIS LINE 
      buttonMode = true; 
      addEventListener(MouseEvent.MOUSE_DOWN, down); 
     } 
     protected function down(event:MouseEvent):void 
     { 
      parent.addChild(this); 
      startDrag(); 
      addEventListener(MouseEvent.MOUSE_UP, up); 
     } 
     protected function up(event:MouseEvent):void 
     { 
      stopDrag(); 
      if(dropTarget) 
      { 
       if(dropTarget.parent.name == "mercury_drop") 
       { 
        x = position.x = 279.95; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "venus_drop") 
       { 
        x = position.x = 342.55; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "earth_drop") 
       { 
        x = position.x = 418.2; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "mars_drop") 
       { 
        x = position.x = 497.6; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "jupiter_drop") 
       { 
        x = position.x = 613.65; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "saturn_drop") 
       { 
        x = position.x = 738.4; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "uranus_drop") 
       { 
        x = position.x = 844.8; 
        y = position.y = 267.15; 
       } 
       else if(dropTarget.parent.name == "neptune_drop") 
       { 
        x = position.x = 939.65; 
        y = position.y = 267.15; 
       } 
       else 
       { 
        x = position.x = 517.2; 
        y = position.y = 35.5; 
       } 
      } 
     } 
    } 
} 

所有我想要的就是让文本“EARTH”看不见的,当我打开运行Flash,仅使用代码。但我无法连接到该影片剪辑“earthText_mc”。这个脚本仅在“earth_mc”处连接..我不知道如何调用其他影片剪辑并使它们可见或不可见,因为我希望它们能够...

+0

earthText_mc是实例MovieClip?在已经阶段? – 2012-08-06 12:00:57

+0

是的,这是...请帮助..我真的需要这个 – 2012-08-06 12:05:02

+0

我只是不知道这一行..我刚刚编码出来.. = D – 2012-08-06 12:08:40

回答

0

已经解决了这个问题。

不久,解决的代码如下。

var earth_text:MovieClip = this.parent.getChildByName("earth_text") as MovieClip; 
earth_text.visible = false; 

但是,这个问题并不完全知道as3.0 DisplayObject的继承结构是一个难以解决的问题。首先,应该对下图有透彻的理解。

enter image description here

this.parent是舞台。

this.parent.getChildByName("earth_text")是DisplayObject。 (请仔细阅读getChildByNames方法的文档。)

但类型转换MovieClip。正如您将看到一个文档,此方法是DisplayObject的实际返回值。所以你应该是类型转换。

(如有的DisplayObject必须被添加到舞台上。下面的代码做你的工作做得更好。)

public function earth() 
{ 
    if(!stage) 
     addEventListener(Event.ADDED_TO_STAGE, init); 
    else 
     init(); 
} 
private function init(e:Event=null):void 
{   
    removeEventListener(Event.ADDED_TO_STAGE, init); 

    var earth_text:MovieClip = this.parent.getChildByName("earth_text") as MovieClip; 

    earth_text.visible = false; 
    buttonMode = true; 
    addEventListener(MouseEvent.MOUSE_DOWN, down); 
} 

然而,所有实例的MovieClip在舞台上。孩子的指数非常重要。所有DisplayObject,因为它们按以下顺序进行。

Stage Load -> Your instance of MovieClip are added sequentially in the order index. 

因此,下面的代码被编写并检查控制台窗口。

Earth.as

var _parent:MovieClip = this.parent as MovieClip; 
for(var i:int = 0; i<_parent.numChildren; i++) 
{ 
    trace("index: " + i + " object: " + _parent.getChildAt(i)); 
} 

原始文件是地球实例的指数为1,所以,如果你运行上面的代码没有找到对象。原因是检查下面的控制台,'将能够理解。 添加了Earth对象的实例,但其余对象尚未加载。所以你必须重新排列对象的顺序。查看修改文件的控制台底部。

原始文件。

index: 0 object: [object Shape] 
index: 1 object: [object earth] 
index: 2 object: null 
index: 3 object: null 
index: 4 object: null 
index: 5 object: null 
index: 6 object: null 
index: 7 object: null 
index: 8 object: null 
index: 9 object: null 
index: 10 object: null 
index: 11 object: null 
index: 12 object: null 
index: 13 object: null 
index: 14 object: null 
index: 15 object: null 
index: 16 object: null 
index: 17 object: null 

how to rearrange instance of MovieClip object?

  1. 在地球舞台对象的点击实例。
  2. CRTL + X
  3. CRTL +移+ V(粘贴在其相同的地方)

P.S:同样地,剩余的对象应具有比earth_text实例的较高折射率。 (在控制台中,索引16是一个earth_text实例。)

修改后的文件。

index: 0 object: [object Shape] 
index: 1 object: [object MovieClip] 
index: 2 object: [object MovieClip] 
index: 3 object: [object MovieClip] 
index: 4 object: [object MovieClip] 
index: 5 object: [object MovieClip] 
index: 6 object: [object MovieClip] 
index: 7 object: [object MovieClip] 
index: 8 object: [object MovieClip] 
index: 9 object: [object venus] 
index: 10 object: [object mercury] 
index: 11 object: [object jupiter] 
index: 12 object: [object mars] 
index: 13 object: [object uranus] 
index: 14 object: [object saturn] 
index: 15 object: [object neptune] 
index: 16 object: [object MovieClip] 
index: 17 object: [object earth] 

实例DisplayObject在使用时一定要小心。在父项中按索引的升序添加。

如果您仍不明白,请发表评论。我很乐意提供帮助。好运

+0

我爱你的解释,现在对我更有意义,多亏了你=)。但我尝试了您提供的代码,但“地球”文本仍然可见。我不知道我做错了什么。 – 2012-08-06 14:24:14

+0

哦等等..我没有安排对象..我的错误... = P – 2012-08-06 14:26:41

+0

你是重新排列实例对象的索引吗? DisplayObject的索引非常非常非常重要。你不要忘记。 我已经运行了代码。没问题。 – 2012-08-06 14:27:03

-1

我不喜欢使用MOUSE_UP事件目的。

我宁愿用它在舞台上,工作就像一个魅力:

function handleMouseDown(e:MouseEvent):void 
{ 
    stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp); 
    // your code... 
} 

private function handleMouseUp(e:MouseEvent):void 
{ 
    stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp); 
    // your code...    
}