AS3阶段=空? [英] AS3 stage = null?
问题描述
我刚刚试图执行一个菜单系统,我看到了一个教程到一个游戏,我的工作,这是一帆风顺,直到我遇到了现在的阶段出现问题被设置为空,我不知道如何阻止它。即不断地刷新它的行是AvoiderGame.as文件是 stage.addEventListener(Event.ADDED_TO_STAGE,INIT);
它保持返回下面的错误
类型错误:错误#1009:无法访问空对象引用的属性或方法。
在AvoiderGame()[M:\用户\刘德华\文档\编程为乐\闪存\教程 - 躲避游戏\职业\ AvoiderGame.as:29]
在美国/改变状态()[M:\用户\刘德华\文档\编程为乐\闪存\教程 - 躲避游戏\ States.as:26]
在MAINMENU / brnP_Button()[M:\用户\刘德华\文档\编程为乐\闪存\教程 - 躲避游戏\职业\ mainMenu.as:34]
目前我编程我的比赛方式是如此,它开始于States.as其中包含下列code。
包
{
导入flash.display使用*。
进口flash.system.fscommand;
公共类国家扩展了影片剪辑
{
公共职能的国家()
{
改变状态(空,菜单);
}
公共职能改变状态(currentState的,nextState)
{
如果(currentState的!= NULL)
{
removeChild之(currentState的);
}
开关(nextState)
{
案菜单:VAR毫米:MAINMENU =新的MainMenu(改变状态);
的addChild(毫米);
打破;
案游戏:VAR G:AvoiderGame =新AvoiderGame(改变状态);
的addChild(G);
打破;
案退出:fscommand的(退出);
打破;
}
}
}
}
从那里将进入mainMenu.as,直到用户点击播放 - 内mainMenu.as是以下code
包
{
导入flash.display使用*。
进口flash.events *。
公共类MAINMENU扩展影片剪辑
{
VAR theCallBackFunction:功能;
公共职能的MainMenu(回调)
{
VAR背景:gameBackground;
背景=新gameBackground();
的addChild(背景);
VAR btnPlay:mmPlay =新mmPlay();
btnPlay.addEventListener(的MouseEvent.MOUSE_DOWN,brnP_Button);
btnPlay.x =宽度/ 2;
btnPlay.y =高度/ 2 - btnPlay.height / 2;
的addChild(btnPlay);
VAR btnExit:mmExit =新mmExit();
btnExit.addEventListener(的MouseEvent.MOUSE_DOWN,brnE_Button);
btnExit.x =宽度/ 2;
btnExit.y =高度/ 2 - btnExit.height / 2;
btnExit.y + = btnExit.height + 4;
的addChild(btnExit);
theCallBackFunction =回调;
}
公共职能brnP_Button(E:的MouseEvent)
{
theCallBackFunction(这个游戏);
返回;
}
公共职能brnE_Button(E:的MouseEvent)
{
theCallBackFunction(这一点,退出);
返回;
}
}
}
现在这是它出了问题 - 它进入AvoiderGame.as然后它踢我回来了,我不知道如何解决错误 - ?谁能告诉我如何解决它。
包
{
进口的flash.display.MovieClip;
进口flash.utils.Timer;
进口对象类型:flash.events.Event;
进口flash.events.TimerEvent;
进口com.freeactionscript.CollisionTest;
进口的flash.display.Stage;
公共类AvoiderGame扩展影片剪辑
{
VAR theCallBackFunction:功能;
公共静态无功enemyArray:阵列;
公共变种的敌人:敌人
公共变种背景:gameBackground;
公共变种头像:头像;
公共变种gameTimer:定时器;
私人VAR _collisionTest:CollisionTest;
私人VAR numStars:INT = 80;
私人VAR fireTimer:定时器; //导致火灾之间的延迟
私人VAR canFire:布尔=真; //你能火激光
公共职能AvoiderGame(回调)
{
stage.addEventListener(Event.ADDED_TO_STAGE,INIT);
theCallBackFunction =回调;
}
私有函数初始化(五:事件):无效
{
背景=新gameBackground();
的addChild(背景);
enemyArray =新的Array();
VAR敌人=新敌人(Math.round(1 +(500 - 1)*的Math.random()), - 2期);
enemyArray.push(敌人);
的addChild(敌人);
化身=新的阿凡达(阶段);
的addChild(化身);
avatar.x = stage.stageWidth / 2;
avatar.y = stage.stageHeight / 2;
对于(VAR我:= 0; I< numStars;我++)
{
stage.addChildAt(新星(阶段),1);
}
_collisionTest =新CollisionTest();
gameTimer =新的定时器(25);
gameTimer.addEventListener(TimerEvent.TIMER,onTick);
gameTimer.start();
fireTimer =新定时器(300,1);
fireTimer.addEventListener(TimerEvent.TIMER,fireTimerHandler,假,0,真正的);
fireTimer.start();
}
公共职能onTick(timerEvent:TimerEvent):无效
{
如果(的Math.random()&所述; 0.1)
{
跟踪(数组的长度:,AvoiderGame.enemyArray.length);
敌人=新敌人(Math.round(1 +(500 - 1)*的Math.random()), - 28级);
enemyArray.push(敌人);
的addChild(敌人);
}
avatar.UpdateAvatar(canFire);
如果(canFire ==真)
{
canFire = FALSE;
fireTimer.start();
}
avatar.StayOnScreen();
每个(VAR的敌人:敌人enemyArray)
{
enemy.moveDown();
enemy.StayOnScreen();
如果(_collisionTest.complex(敌人,化身))
{
gameTimer.stop();
}
}
}
私有函数fireTimerHandler(E:TimerEvent):无效
{
//定时器跑了,我们就可以再次触发。
canFire =真;
}
}
}
公共职能AvoiderGame(回调)
{
stage.addEventListener(Event.ADDED_TO_STAGE,INIT);
theCallBackFunction =回调;
}
应该是这样的。
公共职能AvoiderGame(回调)
{
this.addEventListener(Event.ADDED_TO_STAGE,INIT);
theCallBackFunction =回调;
}
不要忘记删除事件监听器当初始化函数被调用。
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:\Users\Andy\Documents\Programming For Fun\Flash\Tutorial - Avoider Game\Classes\AvoiderGame.as:29]
at States/changeState()[M:\Users\Andy\Documents\Programming For Fun\Flash\Tutorial - Avoider Game\States.as:26]
at mainMenu/brnP_Button()[M:\Users\Andy\Documents\Programming For Fun\Flash\Tutorial - Avoider Game\Classes\mainMenu.as:34]
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;
}
}
}
}
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;
}
}
}
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;
}
Should be something like
public function AvoiderGame(callBack)
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
theCallBackFunction = callBack;
}
Don't forget to remove the event listener when the init function is called.
这篇关于AS3阶段=空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!