2013-03-07 43 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