访问数组 as3 中特定帧上的影片剪辑
我有一个数组(newStep)中的影片剪辑,它被动态添加到舞台上。每次添加实例时,它都会随机选择要转到的帧。有一个嵌套的影片剪辑 (stepLine),我需要更改其 alpha。该代码实际上适用于将字符串添加到动态文本框(pointsDText),但是当我尝试访问嵌套影片剪辑(stepLine)时,它给了我 1009 空对象引用错误。有趣的是,代码实际上可以工作,并且确实改变了影片剪辑的 Alpha,但我仍然收到该错误,而且我认为这使我的游戏更加出问题。我也尝试过使用 if(contains(steps[r].stepLine)) 但它不起作用。有没有更好的方法来访问该影片剪辑而不出现错误?
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;
}
}
}
这很难解释,但我希望你能理解。
非常感谢。
I have movie clip in an array (newStep) that is being added to the stage dynamically. It's randomly choosing a frame to go to every time an instance is added. There is a nested movie clip (stepLine) that i need to change the alpha of. This code actually works for adding a string to to the dynamic text box (pointsDText) but when i try to access the nested movie clip (stepLine) it gives me 1009 null object reference error. The funny thing is the code actually works and does change the alpha of the movie clip but i still get that error, and i think it's making my game more glitchy. I've tried using if(contains(steps[r].stepLine)) too but it doesn't work. Is there a better way to access this movie clip without getting the error?
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;
}
}
}
This is so difficult to explain but i hope you understand.
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您尝试访问不指向任何对象的变量的属性(空引用)时,就会发生空引用错误。您实际上正在尝试访问一个不存在的对象。例如,其中一个实例中可能不存在
stepLine
,因此stepLine.alpha
导致了错误。 (如何设置不存在的剪辑的 Alpha?)也许steps[r]
剪辑位于尚不存在任何stepLine
MovieClip 的帧上。您应该在 Flash IDE 中按 Ctrl+Shift+Enter 以调试模式运行影片。这应该向您显示导致错误的确切行,并且它可以让您检查该点的任何变量的值。这应该可以帮助您找出问题所在。同样,您可以使用跟踪语句来帮助调试。例如,您可以
trace(steps[r].stepLine);
来检查空值,甚至可以简单地if(!steps[r].stepLine) trace("ERROR") ;
。此外,如果将访问包装在 if 语句中,则可以避免空引用错误,即使这并不能真正解决根本问题:A null reference error happens when you try to access properties of a variable that doesn't point to any object -- a null reference. You're effectively trying to access an object that doesn't exist. For example, perhaps
stepLine
doesn't exist in one of those instances, sostepLine.alpha
is causing the error. (How do you set the alpha of a non-existent clip?) Maybe thesteps[r]
clip is on a frame where there is not yet anystepLine
MovieClip.You should run the movie in debug mode by pressing Ctrl+Shift+Enter in the Flash IDE. This should show you the exact line that causes the error, and it will let you inspect the values of any variables at that point. This should help you track down the problem. Similarly, you can use trace statements to aid in debugging. For example, you could
trace(steps[r].stepLine);
to check for null values, or even simplyif(!steps[r].stepLine) trace("ERROR");
. Additionally, if you wrap your accesses in if statements, you can avoid the null reference errors, even though this doesn't really address the underlying problem: