开发者

Flash - textfield not instancied , need eventlistener to tell me when the class is created

Im creating a game in Flash and I have a class that have some text fields that I need to populate in this case a highScore. I get this error.

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Meaning that the object doesn´t exist , for some reason.

if (highscore.data.hard != null){ 
    highscore_txt.text= String( highscore.data.hard);. 
}

The highscore_txt I have created it , on the design view , meaning timeline.

I have other textfield, that I acess like the score and dont have any problem开发者_如何学编程, this is problable because is acessed and I try to write to the textfield in the constructer of the class. I have done some tests and if I populate the field after all the constructer stuff is done , it works, like when I click some place and call that function using a eventlister.

what can I do , so that only after the class is completed initialized I insert the data in the textfield, is there any event that is listening for the class endes to create itself??


You should use Debugger in your Flash Builder (or other IDE) and set breakpoint to the first line of your code. Then check if highscore isn't null, highscore.data isn't null too.

Or you can change your code to be the following:

if (highscore && highscore.data && highscore.data.hard)  
{ 
    highscore_txt.text= highscore.data.hard.toString(); 
}


Just put an event Listener in your constructor that listens to the ADDED_TO_STAGE-event :)

addEventListener(Event.ADDED_TO_STAGE, yourFunctionToPopulate);

private function yourFunctionToPopulate(e:Event):void
{}


Assuming you have the highscore_txt textfield in the same MovieClip the class is attached to, and you have :

public var highscore_txt : TextField;

somewhere in the class, you should be waiting for the Event.ADDED_TO_STAGE before doing anything with the elements. You can read about all the events here


Create a Bindable variable using [Bindable] annotation. In your text field reference the bindable variable such as below.

[Bindable]
var myBindableVar:String = "";
</script>

<mx:TextInput text="{myBindableVar}" />

Now when you update the var, your text field value will auto-update. You'll need to fix the syntax, but you get the idea?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜