2011-11-30 70 views
0

我正在製作遊戲。我已經設計了一個Flash程序欄,並鏈接到AS 3 在主類(main_c.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類是創建一個按鈕並單擊以激發第三個類(game.as)的構造函數。這裏是start_b的代碼:

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()

這裏是第三類的代碼(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 
    } 

} 

} 

如果你能幫助我,我將:-)感謝和美好的一天很高興!

+0

如果有人有解決方案,請寫下:-) – Mariyan

+2

看起來你已經有了一些很好的答案,但是關於好的編碼實踐只是一些注意事項:1)類名應該大寫並使用UpperCamelCase(與標準Flash庫類被命名,例如:MovieClip)。所以,你的'timer_bar'類應該被命名爲'TimerBar',你的'main_c'類應該被命名爲'MainC'等。2)通常希望使用比單個字符更可讀的人名。你的行's = new start_b();'會比'startBtn = new StartButton();'更具可讀性。無論如何,只是一些指針:) – Ian

回答

0

你需要檢查你的舞臺已準備就緒:

Main_c /構造:

public function main_c() 
    { 
    if (stage) 
    { 
     init(); 
    } 
    else 
    { 
     addEventListener(Event.ADDED_TO_STAGE, init); 
    } 
    } 

Main_c /初始化:

private function init(e:Event = null):void 
{ 
    removeEventListener(Event.ADDED_TO_STAGE, init); 

    stageRef = stage; 
    s = new start_b(); 
    addChild(s); 
    s.x = 260; 
    s.y = 225; 
} 
+0

非常感謝,這項工程;-) – Mariyan

+0

惠康:) ..並永遠標記接受的答案,如果這是對你的問題的正確答案..和其他好的答案,你可以給他們投票。順便說一句,伊恩在評論中有一些很好的提示。 – Jarno

0

線索#1:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at game() at start_b/start_g()

這意味着某些obj game構造函數中的ect爲null,但您試圖訪問該對象的成員函數或屬性。

打破它:

main_c.stageRef.addChild(bar); 
points = 0; 
time = new Timer(50); 
time.addEventListener(TimerEvent.TIMER, flow); 
time.start(); 
trace("d"); 

在這裏的錯誤的唯一可能的原因是第一線

main_c.stageRef.addChild(bar); 

因此,解決這將是看是否main_c.stageRef是空的,採取相應的行動

我解決了這個:重新定義遊戲類的構造函數:

callLater方法

在一個不相關的音符10

public function game() { 
    init(); 
} 

public function init() { 
    if(main_c.stageRef) { 
     //restartirane na igrata (nulirane) 
     main_c.stageRef.addChild(bar); 
     points = 0; 
     time = new Timer(50); 
     time.addEventListener(TimerEvent.TIMER, flow); 
     time.start(); 
     trace("d"); 
    } else { 
     callLater(init); 
    } 
} 

文檔,ActionScript類的名字開始與約定大寫字母。這有助於將它們與以小寫字母開頭的實例名稱區分開來。

+0

非常感謝,我會牢記:-)你真的幫助我,祝你有美好的一天! – Mariyan