AS3 中的错误 1009
我有名为 inputWord 的 TextField 实例,它在第一帧上不包含任何文本。在同一帧上,在动作层上,每当我以任何方式引用inputWord时,都会出现错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DC/frame1()[DC::frame1:19] //DC is the name of document class that I created.
at flash.display::MovieClip/gotoAndStop()
at DC()[C:\Users\nikkka\Desktop\flash\DC.as:25]
19是我涉及inputWord的代码所在的行号。它有效,我的意思是我写的 inputWord.text = "smth"
它的文本变为“smth”,但存在相同的错误。为什么?
I have TextField instance called inputWord which contains no text on the first frame. On the same frame, on the actions layer, any time when I refer to inputWord in any way, there is an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DC/frame1()[DC::frame1:19] //DC is the name of document class that I created.
at flash.display::MovieClip/gotoAndStop()
at DC()[C:\Users\nikkka\Desktop\flash\DC.as:25]
19 is the number of line where my code that involves inputWord is located. It works, I mean I write
inputWord.text = "smth"
it text becomes "smth" but there is the same error. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于
as2 中的 gotoAndStop() ,当你执行 gotoAndStop 时,你可以立即访问帧中的资源,正如 Kevin 指出的,必须首先渲染帧
才能执行此操作,你需要使用 onrender 侦听器当您渲染框架以处理框架相关逻辑时触发。然后您需要使舞台无效,以强制触发渲染。
像这样:
The problem is with gotoAndStop()
in as2, when you do a gotoAndStop you can access the resources in the frame right away, as Kevin pointed out, the frame has to be rendered first
to do this, you need to use an onrender listener to fire when you rendered the frame to deal with the frame related logic. Then you need to invalidate the stage, to force the rendering to fire.
like so:
可能在第一帧上,inputWord 尚未加载,因此您会收到错误。在接下来的帧中,它被加载,因此文本设置成功。解决方案是在设置文本字段之前测试文本字段是否存在:
Probably on the first frame, inputWord is not loaded yet so you get an error. On the next frames, it is loaded so the text is being set successfully. The solution is test for the existence of the text field before setting it: