2011-01-11 5 views
0

test.flaのドキュメントクラス内のムービークリップのプロパティにアクセスすると、Test.as test.flaのライブラリは、私がThing.asプロパティpublic var thingStateのにアクセスしようとした2つのムービークリップThingMovieClipとThing2MovieClipスプライト(またはその逆)

を持っていますプロパティ。キャスティングと呼ばれるものを使用して直接 、 は(??)、

逆に、私は私が試した各道の隣になったエラーをコメントし、インスタンス名、動作しませんので、簡単なように見えた 何か、 を割り当てますどのようにMovieClip Thing2MovieClipにThing.as(これはインスタンス化されているのでTest.asの子です)からアクセスできますか?親クラスのオブジェクトは認識できません。ここに2つのクラスがあります:

パッケージcom.hello.test.test { import flash.display.Sprite; import flash.events.Event; import flash.display.DisplayObject; import flash.display.MovieClip;

public class Test extends Sprite 
{ 
    public var container:Sprite;   
    public var thing:Thing; // a Class that extends Sprite that creates a thingGraphic:ThingMovieClip in it from test.fla library 
    public var thing2Graphic:Thing2MovieClip; // base class is MovieClip, in test.fla library 

    public function Test() 
    {   
     container = new Sprite(); 
     addChild(container);    

     thing = new Thing(); 
     container.addChild(thing); 

     thing2Graphic = new Thing2MovieClip(); 
     addChild(thing2Graphic); // adds a Thing2 to stage I presume 

     addEventListener(Event.ENTER_FRAME, update, false, 0, true); 
    } 

    public function update(e:Event):void 
    { 
     // trace(container.thing.thingState);    // Access of possibly undefined property thing through a reference with static type flash.display:Sprite 
     if(container.thing.thingState == container.thing.thingState.OFF_) 
     { 
      trace("container.thing.thingState = OFF_"); 
     } //1119:Access of possibly undefined property thing through a reference with static type flash.display:Sprite 
     // trace(container.MovieClip(thing).thingState);  //1061: Call to a possibly undefined method MovieClip through a reference with static type flash.display:Sprite 
     // var child:DisplayObject = container.getChildByName(thing); //Implicit coercion of a value type com.hello.test.test:Thing to an unrelated type String 
     // trace(child); 
    } 
}  

}

パッケージcom.hello.test.test {インポートflash.display.Sprite。 import flash.events.MouseEvent;

public class Thing extends Sprite 
{ 
    public var thingGraphic:ThingMovieClip; 
    public static const ON_:String = "on_"; 
    public static const OFF_:String = "off_"; 
    public var thingState:String; 

    public function Thing() 
    { 
     thingState = OFF_; 
     buttonMode = true; 
     useHandCursor = true; 

     thingGraphic = new ThingMovieClip(); 
     thingGraphic.stop(); 
     addChild(thingGraphic); 

     this.addEventListener(MouseEvent.MOUSE_DOWN, mousePressed, false, 0, true); 
    } 

    public function mousePressed(e:MouseEvent):void 
    { 
     trace("pressed"); 
     thingState = ON_; 
     trace("thingState " +thingState); 
     thingGraphic.play(); 

     //this.parent.thing2Graphic.play();  // how can I access the thing2 at Test.as (this Class's parent) 
     // 1119: Access of possibly undefined property thing2 through a reference with static tpe flash.display:DisplayObjectContainer 
     stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased, false, 0, true); 
    } 

    public function mouseReleased(e:MouseEvent):void 
    { 
     thingState = OFF_; 
     trace("thingState " +thingState); 
     stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased); 
     thingGraphic.stop(); 
    } 

    public function update():void 
    { 
     trace("updated"); 
    } 
} 

}

私は、私は、ActionScript 3のいくつかの基本的なもののfundemental理解が欠けていると思うと私はどちらか他のスクリプト/プログラミング言語の他の知識を持っていません。 私がチュートリアルに従うと、彼らはちょうど十分にシンプルに見えますが、次に論理的な使用法であると考えられるものを適用すると、エラーがスローされます。 AS3の簡単な理解、 Foundation AS3.0アニメーション、物事を動かす(素晴らしい本、コンセプトにとどまり、AS3についてはこのような本があったがっていてほしい)、そしてFlash Gamesのエッセンシャルガイド(非常に技術的、そのまま現実に従うことができる)?

おかげで、

答えて

0

、単にメンバ変数を使用します。あなたのThing.asで

trace (thing.thingState); 

を、コンテナの親(parent.parentはオブジェクトをキャスト!)Testへ:

var p:Test = parent.parent as Test; 
p.thing2Graphic.play(); 
+0

ありがとうたくさん(!!)私は、テスト(parent.parent).thing2Graphic.play();また動作します。私はあなたの答えから考えていたので、キャストとそれがなぜ必要なのかを(理解して)読み上げる必要があります。parent.parent.thing2Graphic.play();再び、ありがとう、 – bikutaa

+0

問題はありません。私たちの答えの1つを受け入れて、この質問に回答してください。 – weltraumpirat

+0

'parent.parent'は' DisplayObject'を生成します。厳しいActionscriptで作業するときは、実際に入力を追跡する必要があります。アドビのドキュメント(青いキーワードを強調表示し、特定のヘルプについてはf1を押す)と一緒に構文の強調表示(あなたが「物」などを入力するとポップアップするもの)は、あなたのデータ型を追跡するための道のりになります。 –

0

はここにあなたのコメントのエラーの私の解釈です。

  1. container.thing.propの代わりに、thing.propからプロパティにアクセスしてみてください。
  2. container.MovieClip(thing)の代わりに、var newThing = thing as MovieClipを試してください。
  3. getChildByNameは、String(インスタンス名も機能すると思う)を想定しています。あなたはそれをSpriteクラスを拡張するカスタムクラスにしています。
  4. 親クラス内のMOUSE_DOWNリスナーをthing変数に追加します。あなたのTest.asで
 
//(inside Test.as) 

thing.addEventListener(MouseEvent.MOUSE_DOWN, mousePressed, false, 0, true); 

private function mousePressed(evt:MouseEvent):void 
{ 
    thing2Graphic.play(); 
} 
+0

本当にありがとうございました(!!)私は考えているだろうと私は意図的スプライトは、その経由する必要になる容器に入れ、アクセスものの財産、完全にあなたの答えが、「ロジック」へ私を妄想にさせている!! – bikutaa

+0

ネストされたライブラリ項目を通過するときに、それらにアクセスするには 'parent.child'構文を使用する必要があります。しかし、新しい 'Thing'をインスタンス化(作成)しているので、作成中に使用した変数を参照するだけで済みます。 –

+0

4.元々私はTest.as内のすべてを持っていたので、それを単純なものにしてから決定しました:いくつかのプロパティが必要でしたので、クラスに関するすべてのことをクラス内に入れてみました。たとえそれがペアレント化されたり、何かにネストされたりしても、初心者を助けてくれてありがとう! – bikutaa

関連する問題