我正在使用ActionScript 3创建一个Flash CS5游戏。为简化起见,我在源文件夹的顶层创建了一个文件(Game.as)。我的Game.as文件如下所示:
package {
public class Game {
public static function fail():void {
stop();
var restart:RestartButton = new RestartButton();
addChild(restart);
restart.x = stage.stageWidth/2;
restart.y = stage.stageHeight/2;
Game.createButton(restart, function(e:MouseEvent):void { gotoAndPlay (1, "Title Sequence") });
}
}
}
我应该从一个场景的时间轴上的帧中调用Game.fail ()
,但是我得到了这些编译错误:
Line 11 1180: Call to a possibly undefined method stop.
Line 19 1180: Call to a possibly undefined method gotoAndPlay.
Line 17 1120: Access of undefined property stage.
Line 16 1120: Access of undefined property stage.
Line 14 1180: Call to a possibly undefined method addChild.
为什么会发生这些错误?我该怎么做才能修复它们?
提前感谢您的帮助。
答案 0 :(得分:-1)
问题是你的Game
课程中没有这些方法。
至少有两个很好的理由:
Game
课程需要延长MovieClip
以获得方法stop()
,gotoAndPlay()
,addChild()
和stage
属性。例如,class Game extends MovieClip
会为您提供这些方法。Game extends MovieClip
无法从stop()
范围访问static
等方法,也只能从实例范围(即this
)访问。这是因为static
范围本质上是一个全局代码空间,因此这些方法都没有任何意义:当您说stop()
时,您必须说 实例将停止,ex { {1}}或this.stop()
。静态代码不知道甚至可能有哪些类的实例,因此实例函数无法工作。您需要thatThing.stop()
类的实例,而不仅仅是静态函数。我认为您尝试做的主要是使用Game
作为主时间轴的singleton。你可以这样做:
Game
现在您需要将此类设置为document class。文档类只有一个实例,所以这对你很有用。但是,可以将此类链接到库中的符号,并通过代码(package {
public class Game extends MovieClip {
// global reference to a single instance of the Game
public static game:Game;
public function Game() {
// store a global reference to this instance
// NOTE: bad things will happen if you create more than one Game instance
game = this;
}
// do NOT use static for your methods
public function fail():void {
stop();
var restart:RestartButton = new RestartButton();
addChild(restart);
restart.x = stage.stageWidth/2;
restart.y = stage.stageHeight/2;
createButton(restart, function(e:MouseEvent):void { gotoAndPlay (1, "Title Sequence") });
}
}
}
)或通过在关键帧上放置时间轴实例来实例化它。
最后,要从任何地方调用new Game()
方法,您可以使用Game
实例:
Game.game