2013-03-07 25 views
2

我在ActionScript中創建了一個遊戲。我遇到了actionscript中面向對象編程的問題。我有一個game_fla託管遊戲的圖書館組件。導致問題的一個是飛濺的影片剪輯。在這個影片剪輯中,我有幾個圖層可以動畫和加載徽標和兩個按鈕。在文檔類game.as,我有以下代碼:Actionscript OOP

package{ 
import flash.display.MovieClip; 
public class the_game extends MovieClip { 
    public var splash_screen:splash; 
    public var play_screen:the_game_itself; 
    public var how_to_play_screen:how_to_play; 



    public function the_game() { 
     show_splash(); 
    } 

    public function show_splash() { 
     splash_screen = new splash(this); 
     addChild(splash_screen); 
    } 

    public function play_the_game() { 
     play_screen = new the_game_itself(this,level); 
     remove_splash(); 
     addChild(play_screen); 
    } 
etc.. 

這顯然是指保留有關濺組件的信息splash.as文件。這是splash.as代碼:

package { 
    import flash.display.MovieClip; 
    import flash.display.SimpleButton; 
    import flash.events.MouseEvent; 
    public class splash extends MovieClip { 
    public var main_class:the_game; 
    public function splash(passed_class:the_game) { 
     main_class = passed_class; 
     play_btn.addEventListener(MouseEvent.CLICK, playGame); 
     howToPlay_btn.addEventListener(MouseEvent.CLICK, howToPlay); 

    } 

    public function playGame(event:MouseEvent):void{ 
     main_class.play_the_game(); 
    } 

    public function howToPlay(event:MouseEvent):void{ 
     main_class.how_to_play(); 
    } 

} 

}

我的觀點!我遇到的問題是,當我運行game.fla文件時,出現splash.as文件的編譯器錯誤,提示「1120:訪問未定義屬性play_btn和howToPlay_btn」。像我提到的這些按鈕在影片剪輯splash_mc內。 (都有實例名稱等)。只是不確定我要去哪裏錯了?順便說一下,我最初使用Sprite作爲文件,而不是Movie Clip,但兩者都無法工作。

幫助?請?任何人?

+1

'splash_mc'第一幀上的舞臺上的按鈕?如果它們在較晚的幀中,'splash()'構造函數不能訪問它們。 – 2013-03-07 21:11:09

回答

0

就像在生活中,它的糟糕的OOP讓孩子告訴父母該做什麼。它只應該啓動事件,如果需要,父母可以做出反應。否則,您創建依賴關係。

你做這樣的事情:

//in the parent 
public function show_splash() { 
     splash_screen = new splash();//get rid of this, remember to delete from main constructor 
     splash_screen.addEventListener("PLAY_GAME", play_the_game);//add listener 
     addChild(splash_screen); 
    } 


//in the child you just dispatch the event when you need it 
public function playGame(event:MouseEvent):void{ 
     dispatchEvent(new Event("PLAY_GAME")); 
    } 

然後當工作你做同樣的用how_to_play

您只有在需要幀使用影片剪輯,否則使用Sprite。 此外,有時你無法繞過傳遞父母作爲參數,但然後通過它作爲DisplayObjectContainer甚至更​​好地給它一個setter。

+0

感謝您的回答和評論,我找出了問題所在。我在標誌和按鈕的splash_mc放大時開始有一個小動畫。我會推測出問題的原因是因爲我沒有在影片剪輯的第1幀處提供這些按鈕。兩天,我的成本...! – user2145747 2013-03-08 12:45:17