私はゲームを行っています。私はFlashでプロセスバーを設計し、私はステージにVARを割り当てていますメインクラス(main_c.as)で 3. ASにそれをリンクしていますaddChildが例外をスローする
package {
import flash.display.MovieClip;
import flash.display.Stage;
public class main_c extends MovieClip {
static public var stageRef:Stage;
public var s:start_b;
public var bar:timer_bar;
public function main_c()
{
// constructor code
stageRef = stage;
s = new start_b();
addChild(s);
s.x = 260;
s.y = 225;
}
}
}
、その後にあるstart_bクラスがありますボタンを作成し、クリックして3番目のクラス(game.as)のコンストラクタを起動します。
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class start_b extends SimpleButton {
public var g:game;
public function start_b()
{
// constructor code
this.addEventListener(MouseEvent.CLICK, start_g);
}
public function start_g(e:MouseEvent):void
{
g = new game();
this.removeEventListener(MouseEvent.CLICK, start_g);
this.visible = false;
}
}
そして最後のクラスで私は、ステージを参照して、ステータスバーをにaddChildしたいが、私はエラーを取得する実行するとき -
ここTypeError: Error #1009: Cannot access a property or method of a null object reference. at game() at start_b/start_g()
:ここstart_bのコードがあります第三のクラス(game.as)のコードは次のとおりです。
package{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import main_c;
public class game extends MovieClip {
public var points:Number;
public var ptw:Number;
public var time:Timer;
public var bar:timer_bar = new timer_bar();
public var cnt:main_c;
public function game()
{
//restartirane na igrata (nulirane)
main_c.stageRef.addChild(bar);
points = 0;
time = new Timer(50);
time.addEventListener(TimerEvent.TIMER, flow);
time.start();
trace("d");
}
public function flow(t:TimerEvent):void
{
//code
//bar.y++;
}
public function addPoints():void
{
//function code here
}
public function removePoints():void
{
//function code here
}
public function checkTime():void
{
//function code here
}
public function end():void
{
//function code here
}
}
}
あなたは私を助けることができれば、私は感謝して素敵な一日:-)非常に喜んでいるでしょう!
誰かが解決策を持っていればどうかしてください:-) – Mariyan
あなたは既に良い答えがあるようですが、良いコーディング実践についてのちょっとした注意があります:1)クラス名は大文字にし、UpperCamelCase標準のFlashライブラリクラスの名前はex:MovieClipです。したがって、 'timer_bar'クラスの名前は' TimerBar'とし、 'main_c'クラスの名前は' MainC'などとする必要があります。2)一文字以上の人間が読むことができる名前を使用することが望ましい。あなたの 's = new start_b();'は 'startBtn = new StartButton();'のようにもっと読みやすくなります。とにかく、ちょうどいくつかのポインタ:) – Ian