Actionscript 3 AS3错误1009-我错在哪里?
这就是我得到的错误:Actionscript 3 AS3错误1009-我错在哪里?,actionscript-3,flash,loops,setinterval,Actionscript 3,Flash,Loops,Setinterval,这就是我得到的错误: 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/_tim
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()
看不出我哪里出错了-有人能指出我试图引用空对象引用的地方吗(请:)
我的报告的其余部分如下:
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;
}}
导入flash.events.Event;
导入flash.utils.Timer;
导入flash.events.TimerEvent;
//将其设置为变量,以便以后可以对其进行更改
//日期,以便用户级别可以更改(如有必要)。
var速度=6;
var得分=0;
var评分值=10;
风险等级=1;
var NextLevel=100;
var-EggCount=0;
var-EggMax=15;
btn.addEventListener(MouseEvent.CLICK,记分器);
功能记分员(e:事件):无效{
//加分。
分数=分数+10;
//在文本框中显示分数。
scoreText.text=分数;
}
var eggAdd=设定间隔(addEgg,2000);
函数addEgg(){
//添加到蛋数中(以确保不超过最大值)。
蛋数=蛋数+1;
如果(EggCount您的egg
变量是函数范围的,即它在addEgg()中定义
函数,但您在clickedEgg
和loop
中引用了egg
,它们无法实际访问egg
的值。此外,如果有多个egg,您如何计划跟踪单击了哪个egg,等等?至少,在clickedEgg
函数中使用e.target
找出单击了什么egg,不要忘记清除事件侦听器
function addEgg():void {
if (EggCounter>=EggMax) return; // why adding eggs if there are too many?
var egg:egg_mc=new egg_mc();
// rest of code intact
}
function loop(e:Event):void {
var egg:egg_mc=(e.target as egg_mc);
if (!egg) return; // loop is for eggs only
// rest of function intact
}
function clickedEgg(e:MouseEvent):void {
var egg:egg_mc=(e.target as egg_mc);
if (!egg) return;
// rest of function intact, as we now have a valid egg link
egg.removeEventListener(Event.ENTER_FRAME,loop);
egg.removeEventListener(MouseEvent.CLICK,clickedEgg);
// clear listeners. These are persistent until removed manually!
}