あなたにはいくつかの問題があります。 最初に、静的メソッドを呼び出すには、そのクラスを参照する必要があります。
Bus.addBus();
これは、フラッシュを使用すると、メインクラスの「addBus()」と呼ばれる方法バスクラスの静的メソッドを参照する、とされていないかを知ることができます。
第2に、Bus.addBus()メソッドでは、非静的変数を参照します。これは問題を引き起こす可能性があります。特に、ステージ・オブジェクトを参照します。ステージ・オブジェクトは静的ステージがないため、nullになります。代わりに、ステージへの参照を渡すか、関数から新しいバスを返すことができ、呼び出し側クラスに適切な方法でリストを表示させることができます。
2番目の方法をお勧めします。
さらに、addBus()静的メソッドの計画があるかもしれません。 =========================
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Stage;
public class Bus extends Sprite
{
public function Bus(stageReference:Stage)
{
this.x = stageReference.stageWidth/2;
this.y = stageReference.stageHeight/2;
stageReference.addChild(bus); // This is kind of bad form. Better to let the parent do the adding.
}
}
}
:しかし、私はあなたが簡単にそうようなコンストラクタを通じて、非常に機能性を達成する可能性が指摘う============================応答
編集ActionScriptで
コメントする、静的メソッドは、例外でありますルールではありません。したがって、バスを作成するには、次のようにコードを変更します。コメントはコードを説明しています。
package
{
import Bus;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Stage;
public class Main extends Sprite
{
public function Main()
{
// Add a new member variable to the Main class.
var bus:Bus = new Bus();
// we can call methods of our Bus object.
// This imaginary method would tell the bus to drive for 100 pixels.
bus.drive(100);
// We would add the bus to the display list here
this.addChild(bus);
// Assuming we have access to the stage we position the bus at the center.
if(stage != null){
bus.x = stage.stageWidth/2;
bus.y = stage.stageHeight/2;
}
}
}
}
これは、クラスのインスタンスを作成し、静的メソッドを必要とせずにアクセスする方法です。"new"キーワードは事実上、クラスのコンストラクタメソッドを呼び出すためのショートカットであり、クラスの新しいインスタンスを返します。 "new"を呼び出す親はそのインスタンスを子として持ち、そのすべてのパブリックメソッドとプロパティを呼び出すことができます。
Bus.addBus()、tut tutが見つかりません – Ryan