AS3 Component Parameters always NULL or empty? Here’s an even better fix.

In a previous post I explained how to make a component by adding this to it’s class

[Inspectable(defaultValue='')]
public var myNewComponentParam:String = '';

And then explained that the flash compiler can’t access that component property’s values until AFTER the constructor function has run. Originally I proposed this solution, which waits for the stage to be built and for the playhead to hit the first frame before attempting to grab any component properties.

public function myConstructor() {
	this.addEventListener(Event.EXIT_FRAME, afterEverythingElse,
	false, 0, true);
}
public function afterEverythingElse() {
	this.removeEventListener(Event.EXIT_FRAME, afterEverythingElse);
	trace(myNewComponentParam);
}

A commenter named Charis Bournazos pointed out that this method means we’re getting component properties at the exact moment that the first frame is displayed, and if you’re doing something visual with that property, you often see a jump. Here’s what he (she?) suggested as the fix.

public function myConstructor() {
	this.addFrameScript(0, rightBeforeDrawingTheFirstFrame)
}
public function rightBeforeDrawingTheFirstFrame() {
	trace(myNewComponentParam);
}

As you can see, this calls the rightBeforeDrawingTheFirstFrame() function before first frame, which apparently (I haven’t verified) means no more jump.

Thanks Charis!

2 thoughts on “AS3 Component Parameters always NULL or empty? Here’s an even better fix.”

  1. Excellent post. I was checking constantly this weblog and I am impressed! Very helpful information specifically the ultimate phase :) I take care of such information much. I was seeking this certain information for a long time. Thanks and good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>