这是我得到的错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at ICA_v7_fla::MainTimeline/addEgg()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at SetIntervalTimer/onTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
看不到我哪里出错了 - 有人可以指出我试图引用空对象引用的地方吗:)
我的 AS 的其余部分内容如下:
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
//Set as a variable so that it can be changed at a later
//date so that the user level can change (if necessary).
var eggMoveSpeed=6;
var Score=0;
var ScoreValue=10;
var Level=1;
var NextLevel=100;
var EggCount=0;
var EggMax=15;
btn.addEventListener(MouseEvent.CLICK, scorer);
function scorer(e:Event):void{
//Add to the score.
Score=Score+10;
//Display score in text box.
scoreText.text=Score;
}
var eggAdd = setInterval(addEgg,2000);
function addEgg(){
//Add to the egg count (to ensure maximum is not exceeded).
EggCount=EggCount+1;
if (EggCount<=EggMax){
//Create an object of the egg_mc from the egg class.
var egg:egg_mc = new egg_mc();
//Set the Max and Min WIDTH positions.
var maxWidth = 452;
var minWidth = 98;
//Randomize the position of the egg on the screen - with thanks to http://www.kirupa.com/developer/actionscript/tricks/random.htm
var positionX = Math.floor(Math.random()*(1+maxWidth-minWidth))+minWidth;
//Position the egg on the stage, and add it to the screen
egg.y=400;
egg.x=positionX;
//Add the egg to the stage.
stage.addChild(egg);
//Add a moving loop to the egg.
egg.addEventListener(Event.ENTER_FRAME, loop);
}else{
clearInterval(eggAdd);
}
function loop(e:Event):void{
//Move the egg up the screen.
egg.y-=eggMoveSpeed;
//Check to see if egg has got to the top of the screen - if so, then move the object to the bottom.
if (egg.y<-100){
egg.y=400;
}
}
//Add an event listener to the egg, to see if it has been clicked.
egg.addEventListener(MouseEvent.CLICK, clickedEgg);
function clickedEgg(e:Event):void{
//http://asgamer.com/2009/flash-game-design-basics-adding-library-objects-to-stage-with-as3
//Create an object of the brokenEgg_mc from the broken egg class.
var brokenEgg:brokenEgg_mc = new brokenEgg_mc();
//Position the brokenEgg image wherever the egg Image was.
brokenEgg.y=egg.y;
brokenEgg.x=egg.x;
//Add brokenEgg to stage, and remove the egg image.
stage.addChild(brokenEgg);
stage.removeChild(egg);
//Add to the score.
Score=Score+ScoreValue;
//Display score in text box.
scoreText.text=Score;
//Set LevelCheck Variable to 0 - to recalculate correctly.
var LevelCheck = 0;
//Check the level that the user is currently on by dividing the score by the next level required score.
LevelCheck=Score/NextLevel;
//Setup a variable to use for the actual level to display.
var ActualLevel=0;
//Check to see if the LevelCheck variable has come back less than 1 (i.e. 0.1 or a score of 10).
if (LevelCheck < 1){
//If yes, then set the level to 1, as user is still on level 1.
ActualLevel=0;
} else {
//If not, then round down to the lowest level (1.9 is still level 1!).
ActualLevel=Math.floor(LevelCheck);
}
//Display the Lowest Level to the user.
levelText.text=ActualLevel;
}}
显然,计时器事件循环直到舞台上有 15 个鸡蛋,然后间隔应该停止运行——但这会产生错误。