Error 1009 in AS3

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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?

回答1:

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(); 


回答2:

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


文章来源: Error 1009 in AS3
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!