我正在用ActionScript创建一个游戏。我在actionscript中遇到了面向对象编程的问题。我有一个game_fla,它托管游戏的库组件。引起问题的是一个快闪的电影剪辑。在此影片剪辑中,我有几个层,动画和加载一个标志和两个按钮。在文档类game.as中,我有以下代码:
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:Access of undefined property play_btn and howToPlay_btn“。这些按钮就像我提到的那样,都在影片剪辑splash_mc中。(都有实例名称等)只是不确定我做错了什么?顺便说一下,我最初的as文件使用的是Sprite,而不是Movie Clip,但两者都不起作用。
帮助?请?有没有人?
发布于 2013-03-08 06:43:43
就像在生活中一样,让孩子告诉父母该做什么是不好的。它应该只启动事件,如果有必要,父级可以做出反应。否则,您将创建依赖项。
你可以这样做:
//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执行相同的操作
仅当需要帧时才使用MovieClips,否则使用精灵。此外,有时您无法将父对象作为参数传递,但是您可以将其作为DisplayObjectContainer传递,或者更好地给它一个setter。
https://stackoverflow.com/questions/15280151
复制相似问题