AS3 阶段 = 空? [英] AS3 stage = null?
问题描述
我刚刚尝试实现一个菜单系统,我在我正在开发的游戏中看到了一个教程,一切都很顺利,直到我现在遇到了将舞台设置为空的问题,但我没有不知道怎么阻止.不断打破它的行是在AvoiderGame.as文件中,是stage.addEventListener(Event.ADDED_TO_STAGE, init);
它不断返回以下错误
I've just tried to implement a menu system I saw a tutorial onto a game that I'm working on and it was all smooth sailing until I've now encountered a problem with the stage being set to null and I don't know how to stop it. The line that keeps breaking it is in the AvoiderGame.as file and is the stage.addEventListener(Event.ADDED_TO_STAGE, init);
it keeps returning the following error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at AvoiderGame()[M:UsersAndyDocumentsProgramming For FunFlashTutorial - Avoider GameClassesAvoiderGame.as:29]
at States/changeState()[M:UsersAndyDocumentsProgramming For FunFlashTutorial - Avoider GameStates.as:26]
at mainMenu/brnP_Button()[M:UsersAndyDocumentsProgramming For FunFlashTutorial - Avoider GameClassesmainMenu.as:34]
目前我对游戏进行编程的方式是在 States.as 中启动,其中包含以下代码.
Currently the way I've programmed my game is so that it starts in States.as which contains the following code.
package
{
import flash.display.*;
import flash.system.fscommand;
public class States extends MovieClip
{
public function States()
{
changeState(null, "menu");
}
public function changeState (currentState, nextState)
{
if (currentState != null)
{
removeChild(currentState);
}
switch(nextState)
{
case "menu": var mm:mainMenu = new mainMenu(changeState);
addChild(mm);
break;
case "game": var g:AvoiderGame = new AvoiderGame(changeState);
addChild(g);
break;
case "exit": fscommand("quit");
break;
}
}
}
}
从那里它会进入 mainMenu.as 直到用户点击播放 - 在 mainMenu.as 里面是以下代码
From there it will go into mainMenu.as until the user clicks play - inside mainMenu.as is the following code
package
{
import flash.display.*;
import flash.events.*;
public class mainMenu extends MovieClip
{
var theCallBackFunction:Function;
public function mainMenu(callBack)
{
var Background:gameBackground;
Background = new gameBackground();
addChild(Background);
var btnPlay:mmPlay = new mmPlay();
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, brnP_Button);
btnPlay.x = width/2;
btnPlay.y = height/2 - btnPlay.height/2;
addChild(btnPlay);
var btnExit:mmExit = new mmExit();
btnExit.addEventListener(MouseEvent.MOUSE_DOWN, brnE_Button);
btnExit.x = width/2;
btnExit.y = height/2 - btnExit.height/2;
btnExit.y += btnExit.height + 4;
addChild(btnExit);
theCallBackFunction = callBack;
}
public function brnP_Button(e:MouseEvent)
{
theCallBackFunction(this, "game");
return;
}
public function brnE_Button(e:MouseEvent)
{
theCallBackFunction(this, "exit");
return;
}
}
}
现在这就是它出错的地方——它进入了AvoiderGame.as,然后它把我踢回了一个我不知道如何修复的错误——有人能告诉我如何修复它吗?
Now this is where it goes wrong - it goes into AvoiderGame.as and it then kicks me back with an error which I don't know how to fix - Could anyone advise me on how to fix it?
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;
public class AvoiderGame extends MovieClip
{
var theCallBackFunction:Function;
public static var enemyArray:Array;
public var enemy:Enemy
public var Background:gameBackground;
public var avatar:Avatar;
public var gameTimer:Timer;
private var _collisionTest:CollisionTest;
private var numStars:int = 80;
private var fireTimer:Timer; //causes delay between fires
private var canFire:Boolean = true; //can you fire a laser
public function AvoiderGame(callBack)
{
stage.addEventListener(Event.ADDED_TO_STAGE, init);
theCallBackFunction = callBack;
}
private function init(e:Event):void
{
Background = new gameBackground();
addChild(Background);
enemyArray = new Array();
var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 2, stage);
enemyArray.push(enemy);
addChild(enemy);
avatar = new Avatar(stage);
addChild(avatar);
avatar.x = stage.stageWidth / 2;
avatar.y = stage.stageHeight / 2;
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
_collisionTest = new CollisionTest();
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
fireTimer = new Timer(300, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
fireTimer.start();
}
public function onTick(timerEvent:TimerEvent):void
{
if (Math.random() < 0.1)
{
trace('array length: ', AvoiderGame.enemyArray.length);
enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
enemyArray.push(enemy);
addChild(enemy);
}
avatar.UpdateAvatar(canFire);
if (canFire == true)
{
canFire = false;
fireTimer.start();
}
avatar.StayOnScreen();
for each (var enemy:Enemy in enemyArray)
{
enemy.moveDown();
enemy.StayOnScreen();
if (_collisionTest.complex(enemy, avatar))
{
gameTimer.stop();
}
}
}
private function fireTimerHandler(e:TimerEvent) : void
{
//timer ran, we can fire again.
canFire = true;
}
}
}
推荐答案
public function AvoiderGame(callBack)
{
stage.addEventListener(Event.ADDED_TO_STAGE, init);
theCallBackFunction = callBack;
}
应该是这样的
public function AvoiderGame(callBack)
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
theCallBackFunction = callBack;
}
不要忘记在调用 init 函数时移除事件监听器.
Don't forget to remove the event listener when the init function is called.
这篇关于AS3 阶段 = 空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!