0

这是我的代码。

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”不可见,仅使用代码..但我无法连接到影片剪辑“earthText_mc”。该脚本仅在“earth_mc”处连接..我不知道如何调用其他电影剪辑并使它们按照我的意愿可见或不可见..

4

2 回答 2

0

已经解决了问题。

很快,解决代码如下。

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

但是,在不完全了解as3.0 DisplayObject继承结构的情况下,这个问题是一个难以解决的问题。首先,应该彻底了解下图。

在此处输入图像描述

this.parent是阶段。

this.parent.getChildByName("earth_text")是显示对象。(请仔细查看 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);
}

但是,您在 Stage 中的所有实例 MovieClip。子索引很重要。所有的DisplayObject,因为它们是按以下顺序进行的。

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

因此编写了以下代码并检查了控制台窗口。

地球.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+shift+v(粘贴在同一个地方)

ps:同样,其余对象的索引应高于 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 在使用时必须始终小心。在父级按索引的升序添加。

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

于 2012-08-06T14:02:13.480 回答
-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...            
 }
于 2012-08-06T14:00:24.140 回答