2011-04-03 46 views
0

我有一个动态添加到舞台的数组(newStep)中的影片剪辑。每次添加实例时,它都会随机选择一个框架。有一个嵌套的影片剪辑(stepLine),我需要更改alpha的。此代码实际上用于向动态文本框(pointsDText)添加字符串,但是当我尝试访问嵌套的影片剪辑(stepline)时,它会给我1009空对象引用错误。有趣的是代码的实际工作,并确实改变了影片剪辑的alpha,但我仍然得到这个错误,我认为这使得我的游戏更加糟糕。我试过用if(包含(steps [r] .stepLine)),但它不起作用。有没有更好的方式来访问这个影片剪辑而不会出现错误?访问数组中某个帧的影片剪辑as3

if(newStep != null){ 
    for(var r:int = 0; r<steps.length;r++){ 
     if(steps[r].currentLabel == "points"){ 
      steps[r].pointsDText.text = String(hPoints); 
     } 
     if(steps[r].currentLabel == "special"){ 
      steps[r].stepLine.alpha = sStepAlpha; 
     } 
     if(steps[r].currentLabel == "life"){ 
      steps[r].stepLine.alpha = hStepAlpha; 
     } 
    } 
} 

这很难解释,但我希望你能理解。

非常感谢。

回答

0

当您试图访问不指向任何对象的变量的属性 - 空引用时,会发生空引用错误。您正在有效地尝试访问不存在的对象。例如,​​在其中一个实例中可能不存在,因此stepLine.alpha正在导致该错误。 (您如何设置不存在剪辑的Alpha?)也许steps[r]剪辑位于尚未有任何​​MovieClip的帧上。

您应该在调试模式下通过在Flash IDE中按Ctrl + Shift + Enter来运行电影。这应该向您显示导致错误的确切线条,并且可以让您检查当时任何变量的值。这应该可以帮助你追踪问题。同样,您可以使用trace语句来帮助调试。例如,您可以用trace(steps[r].stepLine);来检查空值,或者甚至可以简单地检查if(!steps[r].stepLine) trace("ERROR");。此外,如果您在if语句中包含访问,则可以避免空引用错误,尽管这并不能真正解决底层问题:

if(newStep != null){ 
    for(var r:int = 0; r<steps.length;r++){ 
     // only touch things if the movieclip actually exists 
     if(steps[r] && steps[r].stepLine){ 
      if(steps[r].currentLabel == "points"){ 
       steps[r].pointsDText.text = String(hPoints); 
      } 
      if(steps[r].currentLabel == "special"){ 
       steps[r].stepLine.alpha = sStepAlpha; 
      } 
      if(steps[r].currentLabel == "life"){ 
       steps[r].stepLine.alpha = hStepAlpha; 
      } 
     } 
    } 
} 
+0

修复了它。非常感谢! – user674528 2011-04-03 14:30:37