2013-04-18 128 views
0

这是我得到的错误:AS3错误1009 - 我哪里错了?

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; 
}} 

显然,定时器事件循环,直到有在舞台上,其中间隔应再停止运转15个蛋 - 但是这会产生错误。

回答

0

egg变量是功能全,即它在addEgg()函数的定义,但你参考eggclickedEggloop,这不必egg的价值实际访问。另外,如果有一个以上的蛋,那么您如何计划追踪哪个蛋被点击等?至少,在clickedEgg函数中使用e.target函数来找出被点击的鸡蛋,并且不要忘记清除事件监听器。

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! 
} 
+0

谢谢你的帮助 - 工作很棒:) –