开发者

Error 1009 in AS3

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?


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜