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

ただし、ステージ内のすべてのインスタンス 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. ステージで Earth オブジェクトのインスタンスをクリックします。
  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 に答える