可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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";