我正在开发一款游戏。我在Flash中设计了一个处理栏并将其链接到AS 3。 在主类(main_c.as)中,我为舞台指定了一个var:
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;
}
}
在最后一节课中我想添加参与舞台的状态栏,但是当我跑步时我得到错误 -
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 :(得分:0)
您需要检查您的舞台是否准备就绪:
Main_c / constructor:
public function main_c()
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
Main_c / init:
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;
}
答案 1 :(得分:0)
线索#1:
TypeError:错误#1009:无法访问空对象引用的属性或方法。 at game()at start_b / start_g()
这意味着game
构造函数中的某个对象为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
是否为空,并采取相应的行动
我的解决方案:重新定义游戏类的构造函数:
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);
}
}
callLater方法的文档
在不相关的注释中,ActionScript类名称按惯例以大写字母开头。这有助于将它们与以小写字母开头的实例名称区分开来。