AS3 中的错误 1009

发布于 2024-11-27 23:18:37 字数 485 浏览 1 评论 0原文

我有名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-12-04 23:18:37

问题在于

as2 中的 gotoAndStop() ,当你执行 gotoAndStop 时,你可以立即访问帧中的资源,正如 Kevin 指出的,必须首先渲染帧

才能执行此操作,你需要使用 onrender 侦听器当您渲染框架以处理框架相关逻辑时触发。然后您需要使舞台无效,以强制触发渲染。

像这样:

stage.addEventListener(Event.RENDER, onRenderStage);
protected function onRenderStage(ev:Event):void {
    inputWord.text = "smth"
    trace(inputWord.text);
}
gotoAndStop(5);
stage.invalidate();

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:

stage.addEventListener(Event.RENDER, onRenderStage);
protected function onRenderStage(ev:Event):void {
    inputWord.text = "smth"
    trace(inputWord.text);
}
gotoAndStop(5);
stage.invalidate();
何处潇湘 2024-12-04 23:18:37

可能在第一帧上,inputWord 尚未加载,因此您会收到错误。在接下来的帧中,它被加载,因此文本设置成功。解决方案是在设置文本字段之前测试文本字段是否存在:

if (this.inputWord) this.inputWord.text = "smth";

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:

if (this.inputWord) this.inputWord.text = "smth";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文